fields.php
4.11 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @package Joomla.Plugin
* @subpackage Content.Fields
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die();
/**
* Plug-in to show a custom field in eg an article
* This uses the {fields ID} syntax
*
* @since 3.7.0
*/
class PlgContentFields extends JPlugin
{
/**
* Plugin that shows a custom field
*
* @param string $context The context of the content being passed to the plugin.
* @param object &$item The item object. Note $article->text is also available
* @param object &$params The article params
* @param int $page The 'page' number
*
* @return void
*
* @since 3.7.0
*/
public function onContentPrepare($context, &$item, &$params, $page = 0)
{
// If the item has a context, overwrite the existing one
if ($context == 'com_finder.indexer' && !empty($item->context))
{
$context = $item->context;
}
elseif ($context == 'com_finder.indexer')
{
// Don't run this plugin when the content is being indexed and we have no real context
return;
}
// Don't run if there is no text property (in case of bad calls) or it is empty
if (empty($item->text))
{
return;
}
// Simple performance check to determine whether bot should process further
if (strpos($item->text, 'field') === false)
{
return;
}
// Register FieldsHelper
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
// Prepare the text
if (isset($item->text))
{
$item->text = $this->prepare($item->text, $context, $item);
}
// Prepare the intro text
if (isset($item->introtext))
{
$item->introtext = $this->prepare($item->introtext, $context, $item);
}
}
/**
* Prepares the given string by parsing {field} and {fieldgroup} groups and replacing them.
*
* @param string $string The text to prepare
* @param string $context The context of the content
* @param object $item The item object
*
* @return string
*
* @since 3.8.1
*/
private function prepare($string, $context, $item)
{
// Search for {field ID} or {fieldgroup ID} tags and put the results into $matches.
$regex = '/{(field|fieldgroup)\s+(.*?)}/i';
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
if (!$matches)
{
return $string;
}
$parts = FieldsHelper::extract($context);
if (count($parts) < 2)
{
return $string;
}
$context = $parts[0] . '.' . $parts[1];
$fields = FieldsHelper::getFields($context, $item, true);
$fieldsById = array();
$groups = array();
// Rearranging fields in arrays for easier lookup later.
foreach ($fields as $field)
{
$fieldsById[$field->id] = $field;
$groups[$field->group_id][] = $field;
}
foreach ($matches as $i => $match)
{
// $match[0] is the full pattern match, $match[1] is the type (field or fieldgroup) and $match[2] the ID and optional the layout
$explode = explode(',', $match[2]);
$id = (int) $explode[0];
$output = '';
if ($match[1] == 'field' && $id)
{
if (isset($fieldsById[$id]))
{
$layout = !empty($explode[1]) ? trim($explode[1]) : $fieldsById[$id]->params->get('layout', 'render');
$output = FieldsHelper::render(
$context,
'field.' . $layout,
array(
'item' => $item,
'context' => $context,
'field' => $fieldsById[$id]
)
);
}
}
else
{
if ($match[2] === '*')
{
$match[0] = str_replace('*', '\*', $match[0]);
$renderFields = $fields;
}
else
{
$renderFields = isset($groups[$id]) ? $groups[$id] : '';
}
if ($renderFields)
{
$layout = !empty($explode[1]) ? trim($explode[1]) : 'render';
$output = FieldsHelper::render(
$context,
'fields.' . $layout,
array(
'item' => $item,
'context' => $context,
'fields' => $renderFields
)
);
}
}
$string = preg_replace("|$match[0]|", addcslashes($output, '\\$'), $string, 1);
}
return $string;
}
}