Skip to content

Commit

Permalink
[4.0] Upgrade column blog layouts to according CSS classes (#31570)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmageddon authored Jan 23, 2021
1 parent fe6cdac commit 6e8f744
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
148 changes: 148 additions & 0 deletions administrator/components/com_admin/script.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

defined('_JEXEC') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Extension\ExtensionHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
Expand All @@ -17,6 +18,8 @@
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Uri\Uri;
use Joomla\Component\Config\Administrator\Model\ComponentModel;
use Joomla\Component\Fields\Administrator\Model\FieldModel;
use Joomla\Database\ParameterType;

Expand Down Expand Up @@ -6632,6 +6635,8 @@ public function postflight($action, $installer)
}
}

$this->convertBlogLayouts();

return true;
}

Expand Down Expand Up @@ -6993,4 +6998,147 @@ private function updateContentTypes(): void
$db->execute();
}
}

/**
* Converts layout parameters for blog / featured views into the according CSS classes.
*
* @return void
*
* @since 4.0.0
*/
private function convertBlogLayouts()
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select(
[
$db->quoteName('m.id'),
$db->quoteName('m.link'),
$db->quoteName('m.params'),
]
)
->from($db->quoteName('#__menu', 'm'))
->leftJoin($db->quoteName('#__extensions', 'e'), $db->quoteName('e.extension_id') . ' = ' . $db->quoteName('m.component_id'))
->where($db->quoteName('e.element') . ' = ' . $db->quote('com_content'));

$menuItems = $db->setQuery($query)->loadAssocList('id');
$contentParams = ComponentHelper::getParams('com_content');

foreach ($menuItems as $id => $menuItem)
{
$view = Uri::getInstance($menuItem['link'])->getVar('view');

if (!in_array($view, ['category', 'categories', 'featured']))
{
continue;
}

$params = json_decode($menuItem['params'], true);

// Don't update parameters if num_columns is unset.
if (!isset($params['num_columns']))
{
continue;
}

$useLocalCols = $params['num_columns'] !== '';

if ($useLocalCols)
{
$nColumns = (int) $params['num_columns'];
}
else
{
$nColumns = (int) $contentParams->get('num_columns', '1');
}

unset($params['num_columns']);

$order = 0;
$useLocalOrder = false;

if (isset($params['multi_column_order']))
{
if ($params['multi_column_order'] !== '')
{
$useLocalOrder = true;
$order = (int) $params['multi_column_order'];
}
else
{
$order = (int) $contentParams->get('multi_column_order', '0');
}

unset($params['multi_column_order']);
}

// Only add CSS class if columns > 1 and a local value was set for columns or order.
if ($nColumns > 1 && ($useLocalOrder || $useLocalCols))
{
// Convert to the according CSS class depending on order = "down" or "across".
$layout = ($order === 0) ? 'masonry-' : 'columns-';

if (strpos($params['blog_class'], $layout) === false)
{
$params['blog_class'] .= ' ' . $layout . $nColumns;
}
}

$newParams = json_encode($params);

$query = $db->getQuery(true)
->update($db->quoteName('#__menu'))
->set($db->quoteName('params') . ' = :params')
->where($db->quoteName('id') . ' = :id')
->bind(':params', $newParams, ParameterType::STRING)
->bind(':id', $id, ParameterType::INTEGER);

$db->setQuery($query)->execute();
}

// Update global parameters for com_content.
$nColumns = $contentParams->get('num_columns');

if ($nColumns !== null || true)
{
$nColumns = (int) $nColumns;
$order = (int) $contentParams->get('multi_column_order', '0');
$params = $contentParams->toArray();

if (!isset($params['blog_class']))
{
$params['blog_class'] = '';
}

// Convert to the according CSS class depending on order = "down" or "across".
$layout = ($order === 0) ? 'masonry-' : 'columns-';

if (strpos($params['blog_class'], $layout) === false && $nColumns > 1)
{
$params['blog_class'] .= ' ' . $layout . $nColumns;
}

unset($params['num_columns']);

$app = Factory::getApplication();
/** @var ComponentModel $configModel */
$configModel = $app->bootComponent('com_config')
->getMVCFactory()
->createModel('Component', 'Administrator', ['ignore_request' => true]);

$query = $db->getQuery(true)
->select($db->quoteName('extension_id'))
->from($db->quoteName('#__extensions'))
->where($db->quoteName('element') . ' = ' . $db->quote('com_content'));

$componentId = $db->setQuery($query)->loadResult();

$data = array(
'id' => $componentId,
'option' => 'com_content',
'params' => $params,
);
$configModel->save($data);
}
}
}
16 changes: 16 additions & 0 deletions administrator/components/com_content/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,22 @@
label="COM_CONTENT_CONFIG_BLOG_SETTINGS_LABEL"
description="COM_CONTENT_CONFIG_BLOG_SETTINGS_DESC"
>
<field
name="blog_class_leading"
type="text"
label="JGLOBAL_BLOG_CLASS_LEADING"
size="3"
validate="CssIdentifier"
/>

<field
name="blog_class"
type="text"
label="JGLOBAL_BLOG_CLASS"
size="3"
validate="CssIdentifier"
/>

<field
name="num_leading_articles"
type="number"
Expand Down
18 changes: 18 additions & 0 deletions components/com_content/tmpl/categories/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@
</fieldset>
<fieldset name="blog" label="JGLOBAL_BLOG_LAYOUT_OPTIONS" description="JGLOBAL_SUBSLIDER_DRILL_CATEGORIES_LABEL">

<field
name="blog_class_leading"
type="text"
label="JGLOBAL_BLOG_CLASS_LEADING"
useglobal="true"
size="3"
validate="CssIdentifier"
/>

<field
name="blog_class"
type="text"
label="JGLOBAL_BLOG_CLASS"
useglobal="true"
size="3"
validate="CssIdentifier"
/>

<field
name="num_leading_articles"
type="number"
Expand Down

0 comments on commit 6e8f744

Please sign in to comment.