[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_fields
   6   *
   7   * @copyright   (C) 2005 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\Filesystem\Folder;
  14  use Joomla\CMS\Filesystem\Path;
  15  use Joomla\CMS\Form\FormField;
  16  use Joomla\CMS\HTML\HTMLHelper;
  17  use Joomla\CMS\Language\Text;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Form Field to display a list of the layouts for a field from
  25   * the extension or template overrides.
  26   *
  27   * @since  3.9.0
  28   */
  29  class FieldLayoutField extends FormField
  30  {
  31      /**
  32       * The form field type.
  33       *
  34       * @var    string
  35       * @since  3.9.0
  36       */
  37      protected $type = 'FieldLayout';
  38  
  39      /**
  40       * Method to get the field input for a field layout field.
  41       *
  42       * @return  string   The field input.
  43       *
  44       * @since   3.9.0
  45       */
  46      protected function getInput()
  47      {
  48          $extension = explode('.', $this->form->getValue('context'));
  49          $extension = $extension[0];
  50  
  51          if ($extension) {
  52              // Get the database object and a new query object.
  53              $db = $this->getDatabase();
  54              $query = $db->getQuery(true);
  55  
  56              // Build the query.
  57              $query->select('element, name')
  58                  ->from('#__extensions')
  59                  ->where('client_id = 0')
  60                  ->where('type = ' . $db->quote('template'))
  61                  ->where('enabled = 1');
  62  
  63              // Set the query and load the templates.
  64              $db->setQuery($query);
  65              $templates = $db->loadObjectList('element');
  66  
  67              // Build the search paths for component layouts.
  68              $component_path = Path::clean(JPATH_SITE . '/components/' . $extension . '/layouts/field');
  69  
  70              // Prepare array of component layouts
  71              $component_layouts = array();
  72  
  73              // Prepare the grouped list
  74              $groups = array();
  75  
  76              // Add "Use Default"
  77              $groups[]['items'][] = HTMLHelper::_('select.option', '', Text::_('JOPTION_USE_DEFAULT'));
  78  
  79              // Add the layout options from the component path.
  80              if (is_dir($component_path) && ($component_layouts = Folder::files($component_path, '^[^_]*\.php$', false, true))) {
  81                  // Create the group for the component
  82                  $groups['_'] = array();
  83                  $groups['_']['id'] = $this->id . '__';
  84                  $groups['_']['text'] = Text::sprintf('JOPTION_FROM_COMPONENT');
  85                  $groups['_']['items'] = array();
  86  
  87                  foreach ($component_layouts as $i => $file) {
  88                      // Add an option to the component group
  89                      $value = basename($file, '.php');
  90                      $component_layouts[$i] = $value;
  91  
  92                      if ($value === 'render') {
  93                          continue;
  94                      }
  95  
  96                      $groups['_']['items'][] = HTMLHelper::_('select.option', $value, $value);
  97                  }
  98              }
  99  
 100              // Loop on all templates
 101              if ($templates) {
 102                  foreach ($templates as $template) {
 103                      $files = array();
 104                      $template_paths = array(
 105                          Path::clean(JPATH_SITE . '/templates/' . $template->element . '/html/layouts/' . $extension . '/field'),
 106                          Path::clean(JPATH_SITE . '/templates/' . $template->element . '/html/layouts/com_fields/field'),
 107                          Path::clean(JPATH_SITE . '/templates/' . $template->element . '/html/layouts/field'),
 108                      );
 109  
 110                      // Add the layout options from the template paths.
 111                      foreach ($template_paths as $template_path) {
 112                          if (is_dir($template_path)) {
 113                              $files = array_merge($files, Folder::files($template_path, '^[^_]*\.php$', false, true));
 114                          }
 115                      }
 116  
 117                      foreach ($files as $i => $file) {
 118                          $value = basename($file, '.php');
 119  
 120                          // Remove the default "render.php" or layout files that exist in the component folder
 121                          if ($value === 'render' || in_array($value, $component_layouts)) {
 122                              unset($files[$i]);
 123                          }
 124                      }
 125  
 126                      if (count($files)) {
 127                          // Create the group for the template
 128                          $groups[$template->name] = array();
 129                          $groups[$template->name]['id'] = $this->id . '_' . $template->element;
 130                          $groups[$template->name]['text'] = Text::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
 131                          $groups[$template->name]['items'] = array();
 132  
 133                          foreach ($files as $file) {
 134                              // Add an option to the template group
 135                              $value = basename($file, '.php');
 136                              $groups[$template->name]['items'][] = HTMLHelper::_('select.option', $value, $value);
 137                          }
 138                      }
 139                  }
 140              }
 141  
 142              // Compute attributes for the grouped list
 143              $attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
 144              $attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
 145  
 146              // Prepare HTML code
 147              $html = array();
 148  
 149              // Compute the current selected values
 150              $selected = array($this->value);
 151  
 152              // Add a grouped list
 153              $html[] = HTMLHelper::_(
 154                  'select.groupedlist',
 155                  $groups,
 156                  $this->name,
 157                  array('id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected)
 158              );
 159  
 160              return implode($html);
 161          }
 162  
 163          return '';
 164      }
 165  }


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