[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Plugin 5 * @subpackage Content.Fields 6 * 7 * @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 10 * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace 11 */ 12 13 use Joomla\CMS\Plugin\CMSPlugin; 14 use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; 15 16 // phpcs:disable PSR1.Files.SideEffects 17 \defined('_JEXEC') or die; 18 // phpcs:enable PSR1.Files.SideEffects 19 20 /** 21 * Plug-in to show a custom field in eg an article 22 * This uses the {fields ID} syntax 23 * 24 * @since 3.7.0 25 */ 26 class PlgContentFields extends CMSPlugin 27 { 28 /** 29 * Plugin that shows a custom field 30 * 31 * @param string $context The context of the content being passed to the plugin. 32 * @param object &$item The item object. Note $article->text is also available 33 * @param object &$params The article params 34 * @param int $page The 'page' number 35 * 36 * @return void 37 * 38 * @since 3.7.0 39 */ 40 public function onContentPrepare($context, &$item, &$params, $page = 0) 41 { 42 // If the item has a context, overwrite the existing one 43 if ($context === 'com_finder.indexer' && !empty($item->context)) { 44 $context = $item->context; 45 } elseif ($context === 'com_finder.indexer') { 46 // Don't run this plugin when the content is being indexed and we have no real context 47 return; 48 } 49 50 // Don't run if there is no text property (in case of bad calls) or it is empty 51 if (empty($item->text)) { 52 return; 53 } 54 55 // Simple performance check to determine whether bot should process further 56 if (strpos($item->text, 'field') === false) { 57 return; 58 } 59 60 // Prepare the text 61 if (isset($item->text)) { 62 $item->text = $this->prepare($item->text, $context, $item); 63 } 64 65 // Prepare the intro text 66 if (isset($item->introtext)) { 67 $item->introtext = $this->prepare($item->introtext, $context, $item); 68 } 69 } 70 71 /** 72 * Prepares the given string by parsing {field} and {fieldgroup} groups and replacing them. 73 * 74 * @param string $string The text to prepare 75 * @param string $context The context of the content 76 * @param object $item The item object 77 * 78 * @return string 79 * 80 * @since 3.8.1 81 */ 82 private function prepare($string, $context, $item) 83 { 84 // Search for {field ID} or {fieldgroup ID} tags and put the results into $matches. 85 $regex = '/{(field|fieldgroup)\s+(.*?)}/i'; 86 preg_match_all($regex, $string, $matches, PREG_SET_ORDER); 87 88 if (!$matches) { 89 return $string; 90 } 91 92 $parts = FieldsHelper::extract($context); 93 94 if (count($parts) < 2) { 95 return $string; 96 } 97 98 $context = $parts[0] . '.' . $parts[1]; 99 $fields = FieldsHelper::getFields($context, $item, true); 100 $fieldsById = array(); 101 $groups = array(); 102 103 // Rearranging fields in arrays for easier lookup later. 104 foreach ($fields as $field) { 105 $fieldsById[$field->id] = $field; 106 $groups[$field->group_id][] = $field; 107 } 108 109 foreach ($matches as $i => $match) { 110 // $match[0] is the full pattern match, $match[1] is the type (field or fieldgroup) and $match[2] the ID and optional the layout 111 $explode = explode(',', $match[2]); 112 $id = (int) $explode[0]; 113 $output = ''; 114 115 if ($match[1] === 'field' && $id) { 116 if (isset($fieldsById[$id])) { 117 $layout = !empty($explode[1]) ? trim($explode[1]) : $fieldsById[$id]->params->get('layout', 'render'); 118 $output = FieldsHelper::render( 119 $context, 120 'field.' . $layout, 121 array( 122 'item' => $item, 123 'context' => $context, 124 'field' => $fieldsById[$id], 125 ) 126 ); 127 } 128 } else { 129 if ($match[2] === '*') { 130 $match[0] = str_replace('*', '\*', $match[0]); 131 $renderFields = $fields; 132 } else { 133 $renderFields = $groups[$id] ?? ''; 134 } 135 136 if ($renderFields) { 137 $layout = !empty($explode[1]) ? trim($explode[1]) : 'render'; 138 $output = FieldsHelper::render( 139 $context, 140 'fields.' . $layout, 141 array( 142 'item' => $item, 143 'context' => $context, 144 'fields' => $renderFields, 145 ) 146 ); 147 } 148 } 149 150 $string = preg_replace("|$match[0]|", addcslashes($output, '\\$'), $string, 1); 151 } 152 153 return $string; 154 } 155 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |