/
displaylist.php
121 lines (101 loc) · 3.11 KB
/
displaylist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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;
}
}