Skip to content

Commit

Permalink
Display List Custom Field
Browse files Browse the repository at this point in the history
  • Loading branch information
roland-d committed Apr 26, 2018
1 parent 08bd3c4 commit 708052f
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 0 deletions.
15 changes: 15 additions & 0 deletions administrator/language/en-GB/en-GB.plg_fields_displaylist.ini
@@ -0,0 +1,15 @@
;
; Displaylist language file
;

PLG_FIELDS_DISPLAYLIST="Fields - Display List"
PLG_FIELDS_DISPLAYLIST_LABEL="Display List (%s)"
PLG_FIELDS_DISPLAYLIST_XML_DESCRIPTION="Plugin to create display list for Joomla custom fields"
PLG_FIELDS_DISPLAYLIST_OUTPUTMODE_LABEL="Output mode"
PLG_DISPLAYLIST_SAMPLE_DESCRIPTION="Set the output mode for the display list"
PLG_FIELDS_DISPLAYLIST_UNORDERED_LIST="Unordered list"
PLG_FIELDS_DISPLAYLIST_ORDERED_LIST="Ordered list"
PLG_FIELDS_DISPLAYLIST_PARAMS_LIST_NAME_LABEL="List Name"
PLG_FIELDS_DISPLAYLIST_PARAMS_LIST_NAME_DESC=""
PLG_FIELDS_DISPLAYLISTS_PARAMS_OPTIONS_LABEL="List Items"
PLG_FIELDS_DISPLAYLISTS_LIST_ITEM_LABEL="List item"
13 changes: 13 additions & 0 deletions administrator/language/en-GB/en-GB.plg_fields_displaylist.sys.ini
@@ -0,0 +1,13 @@
;
; Displaylist language file
;

PLG_FIELDS_DISPLAYLIST="Fields - Display List"
PLG_FIELDS_DISPLAYLIST_LABEL="Display List (%s)"
PLG_FIELDS_DISPLAYLIST_XML_DESCRIPTION="Plugin to create display list for Joomla custom fields"
PLG_FIELDS_DISPLAYLIST_OUTPUTMODE_LABEL="Output mode"
PLG_DISPLAYLIST_SAMPLE_DESCRIPTION="Set the output mode for the display list"
PLG_FIELDS_DISPLAYLIST_UNORDERED_LIST="Unordered list"
PLG_FIELDS_DISPLAYLIST_ORDERED_LIST="Ordered list"
PLG_FIELDS_DISPLAYLIST_PARAMS_LIST_NAME_LABEL="List Name"
PLG_FIELDS_DISPLAYLISTS_PARAMS_OPTIONS_LABEL="Display List Values"
1 change: 1 addition & 0 deletions installation/sql/mysql/joomla.sql
Expand Up @@ -643,6 +643,7 @@ INSERT INTO `#__extensions` (`extension_id`, `package_id`, `name`, `type`, `elem
(478, 0, 'plg_editors-xtd_fields', 'plugin', 'fields', 'editors-xtd', 0, 1, 1, 0, '', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(479, 0, 'plg_sampledata_blog', 'plugin', 'blog', 'sampledata', 0, 1, 1, 0, '', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(480, 0, 'plg_system_sessiongc', 'plugin', 'sessiongc', 'system', 0, 1, 1, 0, '', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(481, 0, 'plg_fields_displaylist', 'plugin', 'displaylist', 'fields', 0, 1, 1, 0, '', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(503, 0, 'beez3', 'template', 'beez3', '', 0, 1, 1, 0, '', '{"wrapperSmall":"53","wrapperLarge":"72","sitetitle":"","sitedescription":"","navposition":"center","templatecolor":"nature"}', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(504, 0, 'hathor', 'template', 'hathor', '', 1, 1, 1, 0, '', '{"showSiteName":"0","colourChoice":"0","boldText":"0"}', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(506, 0, 'protostar', 'template', 'protostar', '', 0, 1, 1, 0, '', '{"templateColor":"","logoFile":"","googleFont":"1","googleFontName":"Open+Sans","fluidContainer":"0"}', '', '', 0, '0000-00-00 00:00:00', 0, 0),
Expand Down
1 change: 1 addition & 0 deletions installation/sql/postgresql/joomla.sql
Expand Up @@ -657,6 +657,7 @@ INSERT INTO "#__extensions" ("extension_id", "package_id", "name", "type", "elem
(478, 0, 'plg_editors-xtd_fields', 'plugin', 'fields', 'editors-xtd', 0, 1, 1, 0, '', '', '', '', 0, '1970-01-01 00:00:00', 0, 0),
(479, 0, 'plg_sampledata_blog', 'plugin', 'blog', 'sampledata', 0, 1, 1, 0, '', '', '', '', 0, '1970-01-01 00:00:00', 0, 0),
(480, 0, 'plg_system_sessiongc', 'plugin', 'sessiongc', 'system', 0, 1, 1, 0, '', '', '', '', 0, '1970-01-01 00:00:00', 0, 0),
(481, 0, 'plg_fields_displaylist', 'plugin', 'displaylist', 'fields', 0, 1, 1, 0, '', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(503, 0, 'beez3', 'template', 'beez3', '', 0, 1, 1, 0, '', '{"wrapperSmall":"53","wrapperLarge":"72","sitetitle":"","sitedescription":"","navposition":"center","templatecolor":"nature"}', '', '', 0, '1970-01-01 00:00:00', 0, 0),
(504, 0, 'hathor', 'template', 'hathor', '', 1, 1, 1, 0, '', '{"showSiteName":"0","colourChoice":"0","boldText":"0"}', '', '', 0, '1970-01-01 00:00:00', 0, 0),
(506, 0, 'protostar', 'template', 'protostar', '', 0, 1, 1, 0, '', '{"templateColor":"","logoFile":"","googleFont":"1","googleFontName":"Open+Sans","fluidContainer":"0"}', '', '', 0, '1970-01-01 00:00:00', 0, 0),
Expand Down
1 change: 1 addition & 0 deletions installation/sql/sqlazure/joomla.sql
Expand Up @@ -872,6 +872,7 @@ INSERT INTO "#__extensions" ("extension_id", "package_id", "name", "type", "elem
(478, 0, 'plg_editors-xtd_fields', 'plugin', 'fields', 'editors-xtd', 0, 1, 1, 0, '', '', '', '', 0, '1900-01-01 00:00:00', 0, 0),
(479, 0, 'plg_sampledata_blog', 'plugin', 'blog', 'sampledata', 0, 1, 1, 0, '', '', '', '', 0, '1900-01-01 00:00:00', 0, 0),
(480, 0, 'plg_system_sessiongc', 'plugin', 'sessiongc', 'system', 0, 1, 1, 0, '', '', '', '', 0, '1900-01-01 00:00:00', 0, 0),
(481, 0, 'plg_fields_displaylist', 'plugin', 'displaylist', 'fields', 0, 1, 1, 0, '', '', '', '', 0, '0000-00-00 00:00:00', 0, 0),
(503, 0, 'beez3', 'template', 'beez3', '', 0, 1, 1, 0, '', '{"wrapperSmall":"53","wrapperLarge":"72","sitetitle":"","sitedescription":"","navposition":"center","templatecolor":"nature"}', '', '', 0, '1900-01-01 00:00:00', 0, 0),
(504, 0, 'hathor', 'template', 'hathor', '', 1, 1, 1, 0, '', '{"showSiteName":"0","colourChoice":"0","boldText":"0"}', '', '', 0, '1900-01-01 00:00:00', 0, 0),
(506, 0, 'protostar', 'template', 'protostar', '', 0, 1, 1, 0, '', '{"templateColor":"","logoFile":"","googleFont":"1","googleFontName":"Open+Sans","fluidContainer":"0"}', '', '', 0, '1900-01-01 00:00:00', 0, 0),
Expand Down
121 changes: 121 additions & 0 deletions plugins/fields/displaylist/displaylist.php
@@ -0,0 +1,121 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Fields.DisplayList
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\MVC\Model\BaseModel;

defined('_JEXEC') or die;

JLoader::import('components.com_fields.libraries.fieldsplugin', JPATH_ADMINISTRATOR);

/**
* Display list plugin.
*
* @since __DEPLOY_VERSION__
*/
class PlgFieldsDisplayList extends FieldsPlugin
{
/**
* Transforms the field into a DOM XML element and appends it as a child on the given parent.
*
* @param stdClass $field The field.
* @param DOMElement $parent The field node parent.
* @param JForm $form The form.
*
* @return DOMElement
*
* @since __DEPLOY_VERSION__
*/
public function onCustomFieldsPrepareDom($field, DOMElement $parent, JForm $form)
{
$fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form);

if (!$fieldNode)
{
return $fieldNode;
}

$fieldNode->setAttribute('type', 'subform');
$fieldNode->setAttribute('multiple', 'true');
$fieldNode->setAttribute('formsource', '/plugins/fields/displaylist/params/displaylist.xml');
$fieldNode->setAttribute('layout', 'joomla.form.field.subform.repeatable-table');

// Return the node
return $fieldNode;
}

/**
* The save event.
*
* @param string $context The context
* @param JTable $item The article data
* @param boolean $isNew Is new item
* @param array $data The validated data
*
* @return boolean
*
* @since __DEPLOY_VERSION__
*/
public function onContentAfterSave($context, $item, $isNew, $data = array())
{
// Create correct context for category
if ($context == 'com_categories.category')
{
$context = $item->get('extension') . '.categories';

// Set the catid on the category to get only the fields which belong to this category
$item->set('catid', $item->get('id'));
}

// Check the context
$parts = FieldsHelper::extract($context, $item);

if (!$parts)
{
return true;
}

// Compile the right context for the fields
$context = $parts[0] . '.' . $parts[1];

// Loading the fields
$fields = FieldsHelper::getFields($context, $item);

if (!$fields)
{
return true;
}

// Get the fields data
$fieldsData = !empty($data['com_fields']) ? $data['com_fields'] : array();

// Loading the model
/** @var FieldsModelField $model */
$model = BaseDatabaseModel::getInstance('Field', 'FieldsModel', array('ignore_request' => true));

// Loop over the fields
foreach ($fields as $field)
{
// Find the field of this type displaylist
if ($field->type === $this->_name)
{
// Determine the value if it is available from the data
$value = key_exists($field->name, $fieldsData) ? $fieldsData[$field->name] : null;

// Setting the value for the field and the item
$value = json_encode($value);

// Setting the value for the field and the item
$model->setFieldValue($field->id, $item->get('id'), $value);
}
}

return true;
}
}
21 changes: 21 additions & 0 deletions plugins/fields/displaylist/displaylist.xml
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<extension type="plugin" version="3.8.0" group="fields" method="upgrade">
<name>plg_fields_displaylist</name>
<author>Joomla! Project</author>
<creationDate>April 2018</creationDate>
<copyright>Copyright (C) 2005 - 2018 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>__DEPLOY_VERSION__</version>
<description>PLG_FIELDS_DISPLAYLIST_XML_DESCRIPTION</description>
<files>
<filename plugin="displaylist">displaylist.php</filename>
<folder>params</folder>
<folder>tmpl</folder>
</files>
<languages folder="language">
<language tag="en-GB">en-GB/en-GB.plg_fields_displaylist.ini</language>
<language tag="en-GB">en-GB/en-GB.plg_fields_displaylist.sys.ini</language>
</languages>
</extension>
16 changes: 16 additions & 0 deletions plugins/fields/displaylist/params/displaylist.xml
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<form>
<fields>
<fieldset>
<field
name="options"
type="text"
icon="list"
label="PLG_FIELDS_DISPLAYLIST_PARAMS_LIST_NAME_LABEL"
description="PLG_FIELDS_DISPLAYLIST_PARAMS_LIST_NAME_DESC"
multiple="true"
>
</field>
</fieldset>
</fields>
</form>
36 changes: 36 additions & 0 deletions plugins/fields/displaylist/tmpl/displaylist.php
@@ -0,0 +1,36 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Fields.DisplayList
*
* @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('_JEXEC') or die;

$fieldValue = $field->value;

if ($fieldValue === '')
{
return;
}

// Get the values
$fieldValues = json_decode($fieldValue, true);

if (empty($fieldValues))
{
return;
}

$html = '<ul>';

foreach ($fieldValues as $value)
{
$html .= '<li>' . $value['options'] . '</li>';
}

$html .= '</ul>';

echo $html;

0 comments on commit 708052f

Please sign in to comment.