Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[com_fields] Move some events from system plugin to content plugin v2 #14875

Closed
wants to merge 9 commits into from
177 changes: 177 additions & 0 deletions plugins/content/fields/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

defined('_JEXEC') or die();

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;
use Joomla\Registry\Registry;

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

/**
* Plug-in to show a custom field in eg an article
* This uses the {fields ID} syntax
Expand Down Expand Up @@ -166,4 +172,175 @@ private function prepare($string, $context, $item)

return $string;
}

/**
* The display event.
*
* @param string $context The context
* @param stdClass $item The item
* @param Registry $params The params
* @param integer $limitstart The start
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function onContentAfterTitle($context, $item, $params, $limitstart = 0)
{
return $this->display($context, $item, $params, 1);
}

/**
* The display event.
*
* @param string $context The context
* @param stdClass $item The item
* @param Registry $params The params
* @param integer $limitstart The start
*
* @return string
*
& @since __DEPLOY_VERSION__
*/
public function onContentBeforeDisplay($context, $item, $params, $limitstart = 0)
{
return $this->display($context, $item, $params, 2);
}

/**
* The display event.
*
* @param string $context The context
* @param stdClass $item The item
* @param Registry $params The params
* @param integer $limitstart The start
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
public function onContentAfterDisplay($context, $item, $params, $limitstart = 0)
{
return $this->display($context, $item, $params, 3);
}

/**
* Performs the display event.
*
* @param string $context The context
* @param stdClass $item The item
* @param Registry $params The params
* @param integer $displayType The type
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
private function display($context, $item, $params, $displayType)
{
$parts = FieldsHelper::extract($context, $item);

if (!$parts)
{
return '';
}

// If we have a category, set the catid field to fetch only the fields which belong to it
if ($parts[1] == 'categories' && !isset($item->catid))
{
$item->catid = $item->id;
}

$context = $parts[0] . '.' . $parts[1];

// Convert tags
if ($context == 'com_tags.tag' && !empty($item->type_alias))
{
// Set the context
$context = $item->type_alias;

$item = $this->prepareTagItem($item);
}

if (is_string($params) || !$params)
{
$params = new Registry($params);
}

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

if ($fields)
{
$app = Factory::getApplication();

if ($app->isClient('site') && Multilanguage::isEnabled() && isset($item->language) && $item->language == '*')
{
$lang = $app->getLanguage()->getTag();

foreach ($fields as $key => $field)
{
if ($field->language == '*' || $field->language == $lang)
{
continue;
}

unset($fields[$key]);
}
}
}

if ($fields)
{
foreach ($fields as $key => $field)
{
$fieldDisplayType = $field->params->get('display', '2');

if ($fieldDisplayType == $displayType)
{
continue;
}

unset($fields[$key]);
}
}

if ($fields)
{
return FieldsHelper::render(
$context,
'fields.render',
array(
'item' => $item,
'context' => $context,
'fields' => $fields
)
);
}

return '';
}

/**
* Prepares a tag item to be ready for com_fields.
*
* @param stdClass $item The item
*
* @return object
*
* @since __DEPLOY_VERSION__
*/
private function prepareTagItem($item)
{
// Map core fields
$item->id = $item->content_item_id;
$item->language = $item->core_language;

// Also handle the catid
if (!empty($item->core_catid))
{
$item->catid = $item->core_catid;
}

return $item;
}
}
150 changes: 0 additions & 150 deletions plugins/system/fields/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
defined('_JEXEC') or die;

use Joomla\CMS\Form\Form;
use Joomla\Registry\Registry;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

Expand Down Expand Up @@ -307,153 +304,6 @@ public function onContentPrepareForm(JForm $form, $data)
return true;
}

/**
* The display event.
*
* @param string $context The context
* @param stdClass $item The item
* @param Registry $params The params
* @param integer $limitstart The start
*
* @return string
*
* @since 3.7.0
*/
public function onContentAfterTitle($context, $item, $params, $limitstart = 0)
{
return $this->display($context, $item, $params, 1);
}

/**
* The display event.
*
* @param string $context The context
* @param stdClass $item The item
* @param Registry $params The params
* @param integer $limitstart The start
*
* @return string
*
* @since 3.7.0
*/
public function onContentBeforeDisplay($context, $item, $params, $limitstart = 0)
{
return $this->display($context, $item, $params, 2);
}

/**
* The display event.
*
* @param string $context The context
* @param stdClass $item The item
* @param Registry $params The params
* @param integer $limitstart The start
*
* @return string
*
* @since 3.7.0
*/
public function onContentAfterDisplay($context, $item, $params, $limitstart = 0)
{
return $this->display($context, $item, $params, 3);
}

/**
* Performs the display event.
*
* @param string $context The context
* @param stdClass $item The item
* @param Registry $params The params
* @param integer $displayType The type
*
* @return string
*
* @since 3.7.0
*/
private function display($context, $item, $params, $displayType)
{
$parts = FieldsHelper::extract($context, $item);

if (!$parts)
{
return '';
}

// If we have a category, set the catid field to fetch only the fields which belong to it
if ($parts[1] == 'categories' && !isset($item->catid))
{
$item->catid = $item->id;
}

$context = $parts[0] . '.' . $parts[1];

// Convert tags
if ($context == 'com_tags.tag' && !empty($item->type_alias))
{
// Set the context
$context = $item->type_alias;

$item = $this->prepareTagItem($item);
}

if (is_string($params) || !$params)
{
$params = new Registry($params);
}

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

if ($fields)
{
$app = Factory::getApplication();

if ($app->isClient('site') && Multilanguage::isEnabled() && isset($item->language) && $item->language == '*')
{
$lang = $app->getLanguage()->getTag();

foreach ($fields as $key => $field)
{
if ($field->language == '*' || $field->language == $lang)
{
continue;
}

unset($fields[$key]);
}
}
}

if ($fields)
{
foreach ($fields as $key => $field)
{
$fieldDisplayType = $field->params->get('display', '2');

if ($fieldDisplayType == $displayType)
{
continue;
}

unset($fields[$key]);
}
}

if ($fields)
{
return FieldsHelper::render(
$context,
'fields.render',
array(
'item' => $item,
'context' => $context,
'fields' => $fields
)
);
}

return '';
}

/**
* Performs the display event.
*
Expand Down