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->setDocumentHeadDatas();
}
}
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->setDocumentHeadDatas();
}

/**
Expand Down
71 changes: 69 additions & 2 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 @@ -872,4 +876,67 @@ public function setDocumentTitle($title)

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

/**
* Method to add some document head data from view's $params property
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
protected function setDocumentHeadDatas()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data is considered singular/plural. Perhaps change it to setDocumentHeadData.

{
if (isset($this->params) && $this->params instanceof Registry)
{
/* @var Registry $params */
$params = $this->params;
$app = Factory::getApplication();

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

// Check for empty title and add site name if param is set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace lines 897-911 with setDocumentTitle method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is my intention to have these lines of code in the method. If this is accepted, we can set all these head data within single method, no need for using setDocumentTitle method anymore

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 ($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 = $this->params->get('metadata');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where can I set this data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pass it before calling this method. For your information, we are using it here https://github.com/joomla/joomla-cms/blob/staging/components/com_content/views/category/view.html.php#L250-L258


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

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