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

Add method to set document head (meta) data #19234

Closed
wants to merge 9 commits into from
50 changes: 6 additions & 44 deletions libraries/src/MVC/View/CategoriesView.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Joomla\CMS\MVC\View;

use Joomla\CMS\Factory;

defined('JPATH_PLATFORM') or die;

/**
Expand Down Expand Up @@ -56,7 +58,7 @@ public function display($tpl = null)
$items = $this->get('Items');
$parent = $this->get('Parent');

$app = \JFactory::getApplication();
$app = Factory::getApplication();

// Check for errors.
if (count($errors = $this->get('Errors')))
Expand All @@ -66,14 +68,7 @@ public function display($tpl = null)
return false;
}

if ($items === false)
{
$app->enqueueMessage(\JText::_('JGLOBAL_CATEGORY_NOT_FOUND'), 'error');

return false;
}

if ($parent == false)
if ($items === false || $parent === false)
{
$app->enqueueMessage(\JText::_('JGLOBAL_CATEGORY_NOT_FOUND'), 'error');

Expand Down Expand Up @@ -106,11 +101,8 @@ public function display($tpl = null)
*/
protected function prepareDocument()
{
$app = \JFactory::getApplication();
$menus = $app->getMenu();

// Because the application sets a default page title, we need to get it from the menu item itself
$menu = $menus->getActive();
$menu = Factory::getApplication()->getMenu()->getActive();

if ($menu)
{
Expand All @@ -121,36 +113,6 @@ protected function prepareDocument()
$this->params->def('page_heading', \JText::_($this->pageHeading));
}

$title = $this->params->get('page_title', '');

if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = \JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = \JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}

$this->document->setTitle($title);

if ($this->params->get('menu-meta_description'))
{
$this->document->setDescription($this->params->get('menu-meta_description'));
}

if ($this->params->get('menu-meta_keywords'))
{
$this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
}

if ($this->params->get('robots'))
{
$this->document->setMetadata('robots', $this->params->get('robots'));
}
$this->setDocumentHeadData($this->params);
}
}
58 changes: 10 additions & 48 deletions libraries/src/MVC/View/CategoryView.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Joomla\CMS\MVC\View;

use Joomla\CMS\Factory;

defined('JPATH_PLATFORM') or die;

/**
Expand Down Expand Up @@ -107,8 +109,8 @@ class CategoryView extends HtmlView
*/
public function commonCategoryDisplay()
{
$app = \JFactory::getApplication();
$user = \JFactory::getUser();
$app = Factory::getApplication();
$user = Factory::getUser();
$params = $app->getParams();

// Get some data from the models
Expand All @@ -123,20 +125,13 @@ public function commonCategoryDisplay()
$children = $this->get('Children');
$parent = $this->get('Parent');

if ($category == false)
{
return \JError::raiseError(404, \JText::_('JGLOBAL_CATEGORY_NOT_FOUND'));
}

if ($parent == false)
if ($category === false || $parent === false)
{
return \JError::raiseError(404, \JText::_('JGLOBAL_CATEGORY_NOT_FOUND'));
}

// Check whether category access level allows access.
$groups = $user->getAuthorisedViewLevels();

if (!in_array($category->access, $groups))
if (!in_array($category->access, $user->getAuthorisedViewLevels()))
{
return \JError::raiseError(403, \JText::_('JERROR_ALERTNOAUTHOR'));
}
Expand Down Expand Up @@ -165,6 +160,7 @@ public function commonCategoryDisplay()
if ($this->runPlugins)
{
\JPluginHelper::importPlugin('content');
$dispatcher = \JEventDispatcher::getInstance();

foreach ($items as $itemElement)
{
Expand All @@ -174,8 +170,6 @@ public function commonCategoryDisplay()
// For some plugins.
!empty($itemElement->description)? $itemElement->text = $itemElement->description : $itemElement->text = null;

$dispatcher = \JEventDispatcher::getInstance();

$dispatcher->trigger('onContentPrepare', array($this->extension . '.category', &$itemElement, &$itemElement->params, 0));

$results = $dispatcher->trigger('onContentAfterTitle', array($this->extension . '.category', &$itemElement, &$itemElement->core_params, 0));
Expand Down Expand Up @@ -251,13 +245,11 @@ public function display($tpl = null)
*/
protected function prepareDocument()
{
$app = \JFactory::getApplication();
$menus = $app->getMenu();
$app = Factory::getApplication();
$this->pathway = $app->getPathway();
$title = null;

// Because the application sets a default page title, we need to get it from the menu item itself
$this->menu = $menus->getActive();
$this->menu = $app->getMenu()->getActive();

if ($this->menu)
{
Expand All @@ -268,37 +260,7 @@ protected function prepareDocument()
$this->params->def('page_heading', \JText::_($this->defaultPageTitle));
}

$title = $this->params->get('page_title', '');

if (empty($title))
{
$title = $app->get('sitename');
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = \JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = \JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}

$this->document->setTitle($title);

if ($this->params->get('menu-meta_description'))
{
$this->document->setDescription($this->params->get('menu-meta_description'));
}

if ($this->params->get('menu-meta_keywords'))
{
$this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
}

if ($this->params->get('robots'))
{
$this->document->setMetadata('robots', $this->params->get('robots'));
}
$this->setDocumentHeadData($this->params);
}

/**
Expand Down
52 changes: 49 additions & 3 deletions libraries/src/MVC/View/HtmlView.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace Joomla\CMS\MVC\View;

use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Factory;
use Joomla\Registry\Registry;

defined('JPATH_PLATFORM') or die;

/**
Expand Down Expand Up @@ -744,8 +748,8 @@ public function loadHelper($hlp = null)
*/
protected function _setPath($type, $path)
{
$component = \JApplicationHelper::getComponentName();
$app = \JFactory::getApplication();
$component = ApplicationHelper::getComponentName();
$app = Factory::getApplication();

// Clear out the prior search dirs
$this->_path[$type] = array();
Expand Down Expand Up @@ -854,7 +858,7 @@ public function getForm()
*/
public function setDocumentTitle($title)
{
$app = \JFactory::getApplication();
$app = Factory::getApplication();

// Check for empty title and add site name if param is set
if (empty($title))
Expand All @@ -872,4 +876,46 @@ public function setDocumentTitle($title)

$this->document->setTitle($title);
}

/**
* Method to add some document head data from view's $params property
*
* @param Registry $params The view menu parameters
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function setDocumentHeadData(Registry $params)
{
$this->setDocumentTitle($params->get('page_title'));

if ($params->get('menu-meta_description'))
{
$this->document->setDescription($params->get('menu-meta_description'));
}

if ($params->get('menu-meta_keywords'))
{
$this->document->setMetadata('keywords', $params->get('menu-meta_keywords'));
}

if ($params->get('robots'))
{
$this->document->setMetadata('robots', $params->get('robots'));
}

$metaData = $params->get('metadata');

if (is_array($metaData))
{
// Remove empty value
$metaData = array_filter($metaData);

foreach ($metaData as $k => $v)
{
$this->document->setMetadata($k, $v);
}
}
}
}