Skip to content

Commit

Permalink
Fix layoout issue
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Aug 1, 2018
1 parent 7df51bc commit 84feb9e
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected function configureParams($item)
// Merge {{controller.item.name.lower}} params. If this is single-{{controller.item.name.lower}} view, menu params override article params
// Otherwise, {{controller.item.name.lower}} params override menu item params
$active = $app->getMenu()->getActive();
$temp = clone ($data->params);
$temp = clone $data->params;
$item->params = new Registry($item->params);

// Check to see which parameters should take priority
Expand All @@ -136,7 +136,7 @@ protected function configureParams($item)

// If the current view is the active item and an {{controller.item.name.lower}} view for this {{controller.item.name.lower}},
// then the menu item params take priority
if (strpos($currentLink, 'view={{controller.item.name.lower}}') && (strpos($currentLink, '&id=' . (string) $item->id)))
if (strpos($currentLink, 'view={{controller.item.name.lower}}') && strpos($currentLink, '&id=' . $item->id))
{
// $item->params are the {{controller.item.name.lower}} params, $temp are the menu item params
// Merge so that the menu item params take priority
Expand All @@ -147,21 +147,27 @@ protected function configureParams($item)
{
$this->setLayout($active->query['layout']);
}
// Check for alternative layout of article
elseif ($layout = $item->params->get('layout_type'))
{
$this->setLayout($layout);
}
}
else
{
// Current view is not a single {{controller.item.name.lower}}, so the {{controller.item.name.lower}} params take priority here
// Merge the menu item params with the {{controller.item.name.lower}} params so that the {{controller.item.name.lower}} params take priority
$temp->merge($item->params);
$this->params = $temp;

// Check for alternative layouts (since we are not in a single-{{controller.item.name.lower}} menu item)
// Single-{{controller.item.name.lower}} menu item layout takes priority over alt layout for an {{controller.item.name.lower}}
if ($layout = $data->params->get('layout_type'))
// Make sure set layout first since there may no layout params in item
if ($layout = $item->params->get('layout_type'))
{
$this->setLayout($layout);
}

// Current view is not a single {{controller.item.name.lower}}, so the {{controller.item.name.lower}} params take priority here
// Merge the menu item params with the {{controller.item.name.lower}} params so that the {{controller.item.name.lower}} params take priority
$temp->merge($item->params);
$item->params = $temp;

// If not Active, set Title
$this->setTitle($item->get('title'));
}
Expand All @@ -170,11 +176,11 @@ protected function configureParams($item)
{
// Merge so that article params take priority
$temp->merge($data->params);
$this->params = $temp;
$data->params = $temp;

// Check for alternative layouts (since we are not in a single-article menu item)
// Single-article menu item layout takes priority over alt layout for an article
if ($layout = $data->params->get('layout_type'))
if ($layout = $item->params->get('layout_type'))
{
$this->setLayout($layout);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@

<!-- Basic options. -->
<fieldset name="basic"
label="{{extension.element.upper}}_ITEM_SETTINGS">
label="{{extension.element.upper}}_ITEM_SETTINGS"
addfieldpath="/libraries/windwalker/src/Form/Fields">

<field name="layout_type"
type="componentlayout"
type="wwcomponentlayout"
default="default"
extension="{{extension.element.lower}}"
view="{{controller.item.name.lower}}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
<!-- Add fields to the parameters object for the layout. -->
<fields name="params">
<fieldset name="basic"
label="JGLOBAL_CATEGORY_OPTIONS">
label="JGLOBAL_CATEGORY_OPTIONS"
addfieldpath="/libraries/windwalker/src/Form/Fields">

<field name="layout_type"
type="componentlayout"
type="wwcomponentlayout"
label="JFIELD_ALT_LAYOUT"
description="JGLOBAL_FIELD_LAYOUT_DESC"
default="default"
Expand Down
246 changes: 246 additions & 0 deletions src/Form/Fields/wwcomponentlayout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
<?php
/**
* @package Joomla.Legacy
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('JPATH_PLATFORM') or die;

jimport('joomla.filesystem.folder');

/**
* Fork componentlayout field to change view folder to singular.
*
* @since 2.1.17
*/
class JFormFieldWwcomponentlayout extends JFormField
{
/**
* The form field type.
*
* @var string
* @since 2.1.17
*/
protected $type = 'WwcomponentLayout';

/**
* Method to get the field input for a component layout field.
*
* @return string The field input.
*
* @since 2.1.17
*/
protected function getInput()
{
// Get the client id.
$clientId = $this->element['client_id'];

if ($clientId === null && $this->form instanceof JForm)
{
$clientId = $this->form->getValue('client_id');
}

$clientId = (int) $clientId;

$client = JApplicationHelper::getClientInfo($clientId);

// Get the extension.
$extension = (string) $this->element['extension'];

if (empty($extension) && ($this->form instanceof JForm))
{
$extension = $this->form->getValue('extension');
}

$extension = preg_replace('#\W#', '', $extension);

$template = (string) $this->element['template'];
$template = preg_replace('#\W#', '', $template);

$template_style_id = '';
if ($this->form instanceof JForm)
{
$template_style_id = $this->form->getValue('template_style_id');
$template_style_id = preg_replace('#\W#', '', $template_style_id);
}

$view = (string) $this->element['view'];
$view = preg_replace('#\W#', '', $view);

// If a template, extension and view are present build the options.
if ($extension && $view && $client)
{
// Load language file
$lang = JFactory::getLanguage();
$lang->load($extension . '.sys', JPATH_ADMINISTRATOR, null, false, true)
|| $lang->load($extension . '.sys', JPATH_ADMINISTRATOR . '/components/' . $extension, null, false, true);

// Get the database object and a new query object.
$db = JFactory::getDbo();
$query = $db->getQuery(true);

// Build the query.
$query->select('e.element, e.name')
->from('#__extensions as e')
->where('e.client_id = ' . (int) $clientId)
->where('e.type = ' . $db->quote('template'))
->where('e.enabled = 1');

if ($template)
{
$query->where('e.element = ' . $db->quote($template));
}

if ($template_style_id)
{
$query->join('LEFT', '#__template_styles as s on s.template=e.element')
->where('s.id=' . (int) $template_style_id);
}

// Set the query and load the templates.
$db->setQuery($query);
$templates = $db->loadObjectList('element');

// Build the search paths for component layouts.
$component_path = JPath::clean($client->path . '/components/' . $extension . '/view/' . $view . '/tmpl');

// Prepare array of component layouts
$component_layouts = array();

// Prepare the grouped list
$groups = array();

// Add a Use Global option if useglobal="true" in XML file
if ((string) $this->element['useglobal'] === 'true')
{
$groups[JText::_('JOPTION_FROM_STANDARD')]['items'][] = JHtml::_('select.option', '', JText::_('JGLOBAL_USE_GLOBAL'));
}

// Add the layout options from the component path.
if (is_dir($component_path) && ($component_layouts = JFolder::files($component_path, '^[^_]*\.xml$', false, true)))
{
// Create the group for the component
$groups['_'] = array();
$groups['_']['id'] = $this->id . '__';
$groups['_']['text'] = JText::sprintf('JOPTION_FROM_COMPONENT');
$groups['_']['items'] = array();

foreach ($component_layouts as $i => $file)
{
// Attempt to load the XML file.
if (!$xml = simplexml_load_file($file))
{
unset($component_layouts[$i]);

continue;
}

// Get the help data from the XML file if present.
if (!$menu = $xml->xpath('layout[1]'))
{
unset($component_layouts[$i]);

continue;
}

$menu = $menu[0];

// Add an option to the component group
$value = basename($file, '.xml');
$component_layouts[$i] = $value;
$text = isset($menu['option']) ? JText::_($menu['option']) : (isset($menu['title']) ? JText::_($menu['title']) : $value);
$groups['_']['items'][] = JHtml::_('select.option', '_:' . $value, $text);
}
}

// Loop on all templates
if ($templates)
{
foreach ($templates as $template)
{
// Load language file
$lang->load('tpl_' . $template->element . '.sys', $client->path, null, false, true)
|| $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, null, false, true);

$template_path = JPath::clean(
$client->path
. '/templates/'
. $template->element
. '/html/'
. $extension
. '/'
. $view
);

// Add the layout options from the template path.
if (is_dir($template_path) && ($files = JFolder::files($template_path, '^[^_]*\.php$', false, true)))
{
foreach ($files as $i => $file)
{
// Remove layout files that exist in the component folder
if (in_array(basename($file, '.php'), $component_layouts))
{
unset($files[$i]);
}
}

if (count($files))
{
// Create the group for the template
$groups[$template->name] = array();
$groups[$template->name]['id'] = $this->id . '_' . $template->element;
$groups[$template->name]['text'] = JText::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
$groups[$template->name]['items'] = array();

foreach ($files as $file)
{
// Add an option to the template group
$value = basename($file, '.php');
$text = $lang
->hasKey(
$key = strtoupper(
'TPL_'
. $template->name
. '_'
. $extension
. '_'
. $view
. '_LAYOUT_'
. $value
)
)
? JText::_($key) : $value;
$groups[$template->name]['items'][] = JHtml::_('select.option', $template->element . ':' . $value, $text);
}
}
}
}
}

// Compute attributes for the grouped list
$attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
$attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';

// Prepare HTML code
$html = array();

// Compute the current selected values
$selected = array($this->value);

// Add a grouped list
$html[] = JHtml::_(
'select.groupedlist', $groups, $this->name,
array('id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected)
);

return implode($html);
}
else
{
return '';
}
}
}
11 changes: 5 additions & 6 deletions src/View/Engine/AbstractEngine.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Part of Windwalker project.
* Part of Windwalker project.
*
* @copyright Copyright (C) 2016 LYRASOFT. All rights reserved.
* @license GNU General Public License version 2 or later.
Expand Down Expand Up @@ -102,10 +102,11 @@ public function __construct($config = array(), JoomlaContainer $container = null
* @param array $data The data to push into layout.
*
* @return string Rendered layout.
* @throws \Exception
*/
public function render($layout, $data = array())
{
$this->layout = $layout;
$this->setLayout($layout);

return $this->loadTemplate(null, $data);
}
Expand Down Expand Up @@ -149,7 +150,7 @@ public function loadTemplate($tpl = null, $data = null)

if (!$templateFile)
{
throw new \Exception(\Joomla\CMS\Language\Text::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file . '.' . $this->layoutExt), 500);
throw new \Exception(\Joomla\CMS\Language\Text::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file . '.' . $this->layoutExt), 500);
}

$output = $this->execute($templateFile, $data);
Expand Down Expand Up @@ -227,9 +228,7 @@ public function getPath($layout)
$file = \JPath::clean($layout . '.' . $this->layoutExt);

// Find the layout file path.
$path = \JPath::find(clone($this->paths), $file);

return $path;
return \JPath::find(clone $this->paths, $file);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/View/Html/AbstractHtmlView.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ abstract class AbstractHtmlView extends AbstractView
* @param Container $container DI Container.
* @param array $config View config.
* @param \SplPriorityQueue $paths Paths queue.
*
* @throws \Exception
*/
public function __construct(Model $model = null, Container $container = null, $config = array(), \SplPriorityQueue $paths = null)
{
Expand Down

0 comments on commit 84feb9e

Please sign in to comment.