Skip to content

Commit

Permalink
Custom Fields toggle display on read only rights (joomla#20068)
Browse files Browse the repository at this point in the history
* [com_fields] Normalise the request com_fields data (joomla#19884)

* Normalise the request com_fields data

* CS

* PHP 5.3 compat

* Fields in com_fields array (#9)

Fields should be set in com_fields array and not direcly in $data

* Spelling

* Also normalise request data on front-end user profile save (#10)

* Also normalise request data on front-end user profile save

* correct context and option

* Handle 0 properly in empty check

* Simplify

* allowing value 0 to be saved (#11)

when setting a value of 0 in a text field the function empty will return true > setting the value to null

* correct needsUpdate when strlen (or count) = 1 which incorrectly equa… (#12)

* correct needsUpdate when strlen (or count) = 1 which incorrectly equaled to 'true'

* Update field.php

* Update field.php

* Custom fields view on form via toggle on read-only rights

* fix back-end new article

* first / seperate check on read-only access

* refactor code so show_on parameter is part of helper function

* implement inherit value in fields + language things

* loadmodel only when needed

* changed function comment

* change values order so default value (inherit) is displayed first

* Must use self:: for local static member reference
  • Loading branch information
Ruud68 authored and mbabker committed Apr 7, 2018
1 parent df11df4 commit 0c82311
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 10 deletions.
46 changes: 46 additions & 0 deletions administrator/components/com_fields/helpers/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,52 @@ public static function canEditFieldValue($field)
return JFactory::getUser()->authorise('core.edit.value', $parts[0] . '.field.' . (int) $field->id);
}

/**
* Return a boolean based on field (and field group) display / show_on settings
*
* @param stdClass $field The field
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public static function displayFieldOnForm($field)
{
$app = JFactory::getApplication();

// Detect if the field should be shown at all
if ($field->params->get('show_on') == 1 && $app->isClient('administrator'))
{
return false;
}
elseif ($field->params->get('show_on') == 2 && $app->isClient('site'))
{
return false;
}

if (!self::canEditFieldValue($field))
{
$fieldDisplayReadOnly = $field->params->get('display_readonly', '2');

if ($fieldDisplayReadOnly == '2')
{
// Inherit from field group display read-only setting
$groupModel = JModelLegacy::getInstance('Group', 'FieldsModel', array('ignore_request' => true));
$groupDisplayReadOnly = $groupModel->getItem($field->group_id)->params->get('display_readonly', '1');
$fieldDisplayReadOnly = $groupDisplayReadOnly;
}

if ($fieldDisplayReadOnly == '0')
{
// Do not display field on form when field is read-only
return false;
}
}

// Display field on form
return true;
}

/**
* Adds Count Items for Category Manager.
*
Expand Down
10 changes: 2 additions & 8 deletions administrator/components/com_fields/libraries/fieldsplugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,8 @@ public function onCustomFieldsPrepareDom($field, DOMElement $parent, JForm $form
return null;
}

$app = JFactory::getApplication();

// Detect if the field should be shown at all
if ($field->params->get('show_on') == 1 && $app->isClient('administrator'))
{
return;
}
elseif ($field->params->get('show_on') == 2 && $app->isClient('site'))
// Detect if the field is configured to be displayed on the form
if (!FieldsHelper::displayFieldOnForm($field))
{
return null;
}
Expand Down
13 changes: 13 additions & 0 deletions administrator/components/com_fields/models/forms/field.xml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@
<option value="3">COM_FIELDS_FIELD_DISPLAY_AFTER_DISPLAY</option>
<option value="0">COM_FIELDS_FIELD_DISPLAY_NO_DISPLAY</option>
</field>

<field
name="display_readonly"
type="radio"
label="JFIELD_DISPLAY_READONLY_LABEL"
description="JFIELD_DISPLAY_READONLY_DESC"
class="btn-group btn-group-yesno"
default="2"
>
<option value="2">JGLOBAL_INHERIT</option>
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>
</fieldset>
</fields>
</form>
20 changes: 18 additions & 2 deletions administrator/components/com_fields/models/forms/group.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
filter="user_utc"
/>

<field
name="modified_by"
<field
name="modified_by"
type="user"
label="JGLOBAL_FIELD_MODIFIED_BY_LABEL"
class="readonly"
Expand Down Expand Up @@ -150,4 +150,20 @@
class="inputbox"
/>
</fieldset>

<fields name="params" label="COM_FIELDS_FIELD_BASIC_LABEL">
<fieldset name="basic">
<field
name="display_readonly"
type="radio"
label="JFIELD_DISPLAY_READONLY_LABEL"
description="JFIELD_DISPLAY_READONLY_DESC"
class="btn-group btn-group-yesno"
default="1"
>
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>
</fieldset>
</fields>
</form>
12 changes: 12 additions & 0 deletions administrator/components/com_fields/models/group.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
defined('_JEXEC') or die;

use Joomla\Registry\Registry;

/**
* Group Model
*
Expand Down Expand Up @@ -69,6 +71,11 @@ public function save($data)
*/
public function getTable($name = 'Group', $prefix = 'FieldsTable', $options = array())
{
if (strpos(JPATH_COMPONENT, 'com_fields') === false)
{
$this->addTablePath(JPATH_ADMINISTRATOR . '/components/com_fields/tables');
}

return JTable::getInstance($name, $prefix, $options);
}

Expand Down Expand Up @@ -314,6 +321,11 @@ public function getItem($pk = null)
$item->context = $this->getState('filter.context');
}

if (property_exists($item, 'params'))
{
$item->params = new Registry($item->params);
}

// Convert the created and modified dates to local user time for display in the form.
$tz = new DateTimeZone(JFactory::getApplication()->get('offset'));

Expand Down
28 changes: 28 additions & 0 deletions administrator/components/com_fields/models/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
defined('_JEXEC') or die;

use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;

/**
Expand Down Expand Up @@ -213,4 +214,31 @@ protected function getListQuery()

return $query;
}

/**
* Gets an array of objects from the results of database query.
*
* @param string $query The query.
* @param integer $limitstart Offset.
* @param integer $limit The number of records.
*
* @return array An array of results.
*
* @since __DEPLOY_VERSION__
* @throws RuntimeException
*/
protected function _getList($query, $limitstart = 0, $limit = 0)
{
$result = parent::_getList($query, $limitstart, $limit);

if (is_array($result))
{
foreach ($result as $group)
{
$group->params = new Registry($group->params);
}
}

return $result;
}
}
2 changes: 2 additions & 0 deletions administrator/language/en-GB/en-GB.ini
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ JFIELD_BASIS_LOGOUT_DESCRIPTION_LABEL="Logout Description Text"
JFIELD_BASIS_LOGOUT_DESCRIPTION_SHOW_DESC="Show or hide logout description."
JFIELD_BASIS_LOGOUT_DESCRIPTION_SHOW_LABEL="Logout Description"
JFIELD_CATEGORY_DESC="The category that this item is assigned to. You may select an existing category or enter a new category by typing the name in the field and pressing enter."
JFIELD_DISPLAY_READONLY_DESC="Whether to display the field on forms when read-only. Inherit defaults to value set in field group."
JFIELD_DISPLAY_READONLY_LABEL="Display When Read-Only"
JFIELD_ENABLED_DESC="The enabled status of this item."
JFIELD_FIELDS_CATEGORY_DESC="Select the category that this field is assigned to."
JFIELD_KEY_REFERENCE_DESC="Used to store information referring to an external resource."
Expand Down

0 comments on commit 0c82311

Please sign in to comment.