Skip to content

Commit

Permalink
[com_fields] Add a callback for context mapping (joomla#12968)
Browse files Browse the repository at this point in the history
* Add a callback for context mapping

* Simplify stuff

* Move to component helper class, if the section is not known ignore it

* Change function name to validateSection
  • Loading branch information
laoneo authored and rdeutz committed Jan 5, 2017
1 parent 2173ac3 commit 81aad30
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 65 deletions.
27 changes: 27 additions & 0 deletions administrator/components/com_contact/helpers/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,33 @@ public static function countTagItems(&$items, $extension)
return $items;
}

/**
* Returns a valid section for contacts. If it is not valid then null
* is returned.
*
* @param string $section The section to get the mapping for
*
* @return string|null The new section
*
* @since __DEPLOY_VERSION__
*/
public static function validateSection($section)
{
if (JFactory::getApplication()->isClient('site') && $section == 'contact')
{
// The contact form needs to be the mail section
$section = 'mail';
}

if ($section != 'mail' && $section != 'contact')
{
// We don't know other sections
return null;
}

return $section;
}

/**
* Returns valid contexts
*
Expand Down
35 changes: 35 additions & 0 deletions administrator/components/com_content/helpers/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,41 @@ public static function countTagItems(&$items, $extension)
return $items;
}

/**
* Returns a valid section for articles. If it is not valid then null
* is returned.
*
* @param string $section The section to get the mapping for
*
* @return string|null The new section
*
* @since __DEPLOY_VERSION__
*/
public static function validateSection($section)
{
if (JFactory::getApplication()->isClient('site'))
{
// On the front end we need to map some sections
switch ($section)
{
// Editing an article
case 'form':

// Category list view
case 'category':
$section = 'article';
}
}

if ($section != 'article')
{
// We don't know other sections
return null;
}

return $section;
}

/**
* Returns valid contexts
*
Expand Down
22 changes: 22 additions & 0 deletions administrator/components/com_fields/helpers/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ public static function extract($contextString)
return null;
}

$component = $parts[0];
$eName = str_replace('com_', '', $component);

$path = JPath::clean(JPATH_ADMINISTRATOR . '/components/' . $component . '/helpers/' . $component . '.php');

if (file_exists($path))
{
$cName = ucfirst($eName) . 'Helper';

JLoader::register($cName, $path);

if (class_exists($cName) && is_callable(array($cName, 'validateSection')))
{
$section = call_user_func_array(array($cName, 'validateSection'), array($parts[1]));

if ($section)
{
$parts[1] = $section;
}
}
}

return $parts;
}

Expand Down
31 changes: 31 additions & 0 deletions administrator/components/com_users/helpers/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,37 @@ public static function countTagItems(&$items, $extension)
return $items;
}

/**
* Returns a valid section for users. If it is not valid then null
* is returned.
*
* @param string $section The section to get the mapping for
*
* @return string|null The new section
*
* @since __DEPLOY_VERSION__
*/
public static function validateSection($section)
{
if (JFactory::getApplication()->isClient('site'))
{
switch ($section)
{
case 'registration':
case 'profile':
$section = 'user';
}
}

if ($section != 'user')
{
// We don't know other sections
return null;
}

return $section;
}

/**
* Returns valid contexts
*
Expand Down
2 changes: 1 addition & 1 deletion components/com_contact/models/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected function populateState()
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_contact.mail', 'contact', array('control' => 'jform', 'load_data' => true));
$form = $this->loadForm('com_contact.contact', 'contact', array('control' => 'jform', 'load_data' => true));

if (empty($form))
{
Expand Down
73 changes: 9 additions & 64 deletions plugins/system/fields/fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class PlgSystemFields extends JPlugin
*/
public function onContentBeforeSave($context, $item, $isNew)
{
$parts = $this->getParts($context);

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

if (!$parts)
{
Expand Down Expand Up @@ -112,7 +113,7 @@ public function onContentBeforeSave($context, $item, $isNew)
*/
public function onContentAfterSave($context, $item, $isNew)
{
$parts = $this->getParts($context);
$parts = FieldsHelper::extract($context);

if (!$parts)
{
Expand Down Expand Up @@ -223,7 +224,7 @@ public function onUserAfterSave($userData, $isNew, $success, $msg)
*/
public function onContentAfterDelete($context, $item)
{
$parts = $this->getParts($context);
$parts = FieldsHelper::extract($context);

if (!$parts)
{
Expand Down Expand Up @@ -273,7 +274,8 @@ public function onUserAfterDelete($user, $succes, $msg)
public function onContentPrepareForm(JForm $form, $data)
{
$context = $form->getName();
$parts = $this->getParts($context);

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

if (!$parts)
{
Expand Down Expand Up @@ -312,7 +314,7 @@ public function onContentPrepareForm(JForm $form, $data)
*/
public function onContentPrepareData($context, $data)
{
$parts = $this->getParts($context);
$parts = FieldsHelper::extract($context);

if (!$parts)
{
Expand Down Expand Up @@ -390,7 +392,7 @@ public function onContentAfterDisplay($context, $item, $params, $limitstart = 0)
*/
private function display($context, $item, $params, $displayType)
{
$parts = $this->getParts($context);
$parts = FieldsHelper::extract($context);

if (!$parts)
{
Expand Down Expand Up @@ -456,7 +458,7 @@ private function display($context, $item, $params, $displayType)
*/
public function onContentPrepare($context, $item)
{
$parts = $this->getParts($context);
$parts = FieldsHelper::extract($context);

if (!$parts)
{
Expand Down Expand Up @@ -536,61 +538,4 @@ public function onPrepareFinderContent($item)

return true;
}

/**
* Returns the parts for the context.
*
* @param string $context The context
*
* @return array
*
* @since 3.7.0
*/
private function getParts($context)
{
// Some context mapping
// @todo needs to be done in a general lookup table on some point
$mapping = array(
'com_users.registration' => 'com_users.user',
'com_content.category' => 'com_content.article',
);

if (key_exists($context, $mapping))
{
$context = $mapping[$context];
}

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

if (!$parts)
{
return null;
}

if ($parts[1] === 'form')
{
// The context is not from a known one, we need to do a lookup
// @todo use the api here.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('context')
->from('#__fields')
->where('context like ' . $db->quote($parts[0] . '.%'))
->group(array('context'));
$db->setQuery($query);
$tmp = $db->loadObjectList();

if (count($tmp) == 1)
{
$parts = FieldsHelper::extract($tmp[0]->context);

if (count($parts) < 2)
{
return null;
}
}
}

return $parts;
}
}

0 comments on commit 81aad30

Please sign in to comment.