[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_fields/src/Field/ -> SubfieldsField.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_fields
   6   *
   7   * @copyright   (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Fields\Administrator\Field;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Field\ListField;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Fields Subfields. Represents a list field with the options being all possible
  25   * custom field types, except the 'subform' custom field type.
  26   *
  27   * @since  4.0.0
  28   */
  29  class SubfieldsField extends ListField
  30  {
  31      /**
  32       * The name of this Field type.
  33       *
  34       * @var string
  35       *
  36       * @since 4.0.0
  37       */
  38      public $type = 'Subfields';
  39  
  40      /**
  41       * Configuration option for this field type to could filter the displayed custom field instances
  42       * by a given context. Default value empty string. If given empty string, displays all custom fields.
  43       *
  44       * @var string
  45       *
  46       * @since 4.0.0
  47       */
  48      protected $context = '';
  49  
  50      /**
  51       * Array to do a fast in-memory caching of all custom field items. Used to not bother the
  52       * FieldsHelper with a call every time this field is being rendered.
  53       *
  54       * @var array
  55       *
  56       * @since 4.0.0
  57       */
  58      protected static $customFieldsCache = array();
  59  
  60      /**
  61       * Method to get the field options.
  62       *
  63       * @return  array  The field option objects.
  64       *
  65       * @since   4.0.0
  66       */
  67      protected function getOptions()
  68      {
  69          $options = parent::getOptions();
  70  
  71          // Check whether we have a result for this context yet
  72          if (!isset(static::$customFieldsCache[$this->context])) {
  73              static::$customFieldsCache[$this->context] = FieldsHelper::getFields($this->context, null, false, null, true);
  74          }
  75  
  76          // Iterate over the custom fields for this context
  77          foreach (static::$customFieldsCache[$this->context] as $customField) {
  78              // Skip our own subform type. We won't have subform in subform.
  79              if ($customField->type == 'subform') {
  80                  continue;
  81              }
  82  
  83              $options[] = HTMLHelper::_(
  84                  'select.option',
  85                  $customField->id,
  86                  ($customField->title . ' (' . $customField->type . ')')
  87              );
  88          }
  89  
  90          // Sorting the fields based on the text which is displayed
  91          usort(
  92              $options,
  93              function ($a, $b) {
  94                  return strcmp($a->text, $b->text);
  95              }
  96          );
  97  
  98          if (count($options) == 0) {
  99              Factory::getApplication()->enqueueMessage(Text::_('COM_FIELDS_NO_FIELDS_TO_CREATE_SUBFORM_FIELD_WARNING'), 'warning');
 100          }
 101  
 102          return $options;
 103      }
 104  
 105      /**
 106       * Method to attach a JForm object to the field.
 107       *
 108       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 109       * @param   mixed              $value    The form field value to validate.
 110       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 111       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 112       *                                       full field name would end up being "bar[foo]".
 113       *
 114       * @return  boolean  True on success.
 115       *
 116       * @since   4.0.0
 117       */
 118      public function setup(\SimpleXMLElement $element, $value, $group = null)
 119      {
 120          $return = parent::setup($element, $value, $group);
 121  
 122          if ($return) {
 123              $this->context = (string) $this->element['context'];
 124          }
 125  
 126          return $return;
 127      }
 128  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer