Skip to content

Commit

Permalink
[com_fields] Fix fields display HTML prepared 4 or 5 times per articl…
Browse files Browse the repository at this point in the history
…e, make it be prepared only twice (#17895)

* Pass field displayType (aka event type) to getFields

* Update getFields to respect the 'display' parameter of every field

* Update onContentPrepare to respect 'display' parameter of every field

* Prepare for manual display

* Do not create $item->jcfields multiple times

* Revert the code for manual display to always prepare the field value

* Wrong function name

* Fix docblock

* Better comment for parameter of getFields method
  • Loading branch information
ggppdk authored and Michael Babker committed Mar 29, 2018
1 parent 9b0ad04 commit 59fff4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions administrator/components/com_fields/helpers/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function extract($contextString, $item = null)
*
* @param string $context The context of the content passed to the helper
* @param stdClass $item item
* @param boolean $prepareValue prepareValue
* @param int|bool $prepareValue (if int is display event): 1 - AfterTitle, 2 - BeforeDisplay, 3 - AfterDisplay, 0 - OFF
* @param array $valuesToOverride The values to override
*
* @return array
Expand Down Expand Up @@ -187,7 +187,8 @@ function ($f)

$field->rawvalue = $field->value;

if ($prepareValue)
// If boolean prepare, if int, it is the event type: 1 - After Title, 2 - Before Display, 3 - After Display, 0 - Do not prepare
if ($prepareValue && (is_bool($prepareValue) || $prepareValue === (int) $field->params->get('display', '2')))
{
JPluginHelper::importPlugin('fields');

Expand Down
10 changes: 9 additions & 1 deletion plugins/system/fields/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ private function display($context, $item, $params, $displayType)
$params = new Registry($params);
}

$fields = FieldsHelper::getFields($context, $item, true);
$fields = FieldsHelper::getFields($context, $item, $displayType);

if ($fields)
{
Expand Down Expand Up @@ -450,6 +450,12 @@ private function display($context, $item, $params, $displayType)
*/
public function onContentPrepare($context, $item)
{
// Check property exists (avoid costly & useless recreation), if need to recreate them, just unset the property!
if (isset($item->jcfields))
{
return;
}

$parts = FieldsHelper::extract($context, $item);

if (!$parts)
Expand All @@ -468,6 +474,8 @@ public function onContentPrepare($context, $item)
$item = $this->prepareTagItem($item);
}

// Get item's fields, also preparing their value property for manual display
// (calling plugins events and loading layouts to get their HTML display)
$fields = FieldsHelper::getFields($context, $item, true);

// Adding the fields to the object
Expand Down

0 comments on commit 59fff4e

Please sign in to comment.