[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> MenuitemField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2010 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Field;
  11  
  12  use Joomla\CMS\HTML\HTMLHelper;
  13  use Joomla\Component\Menus\Administrator\Helper\MenusHelper;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Supports an HTML grouped select list of menu item grouped by menu
  21   *
  22   * @since  1.6
  23   */
  24  class MenuitemField extends GroupedlistField
  25  {
  26      /**
  27       * The form field type.
  28       *
  29       * @var    string
  30       * @since  1.6
  31       */
  32      public $type = 'MenuItem';
  33  
  34      /**
  35       * The menu type.
  36       *
  37       * @var    string
  38       * @since  3.2
  39       */
  40      protected $menuType;
  41  
  42      /**
  43       * The client id.
  44       *
  45       * @var    string
  46       * @since  3.2
  47       */
  48      protected $clientId;
  49  
  50      /**
  51       * The language.
  52       *
  53       * @var    array
  54       * @since  3.2
  55       */
  56      protected $language;
  57  
  58      /**
  59       * The published status.
  60       *
  61       * @var    array
  62       * @since  3.2
  63       */
  64      protected $published;
  65  
  66      /**
  67       * The disabled status.
  68       *
  69       * @var    array
  70       * @since  3.2
  71       */
  72      protected $disable;
  73  
  74      /**
  75       * Method to get certain otherwise inaccessible properties from the form field object.
  76       *
  77       * @param   string  $name  The property name for which to get the value.
  78       *
  79       * @return  mixed  The property value or null.
  80       *
  81       * @since   3.2
  82       */
  83      public function __get($name)
  84      {
  85          switch ($name) {
  86              case 'menuType':
  87              case 'clientId':
  88              case 'language':
  89              case 'published':
  90              case 'disable':
  91                  return $this->$name;
  92          }
  93  
  94          return parent::__get($name);
  95      }
  96  
  97      /**
  98       * Method to set certain otherwise inaccessible properties of the form field object.
  99       *
 100       * @param   string  $name   The property name for which to set the value.
 101       * @param   mixed   $value  The value of the property.
 102       *
 103       * @return  void
 104       *
 105       * @since   3.2
 106       */
 107      public function __set($name, $value)
 108      {
 109          switch ($name) {
 110              case 'menuType':
 111                  $this->menuType = (string) $value;
 112                  break;
 113  
 114              case 'clientId':
 115                  $this->clientId = (int) $value;
 116                  break;
 117  
 118              case 'language':
 119              case 'published':
 120              case 'disable':
 121                  $value = (string) $value;
 122                  $this->$name = $value ? explode(',', $value) : array();
 123                  break;
 124  
 125              default:
 126                  parent::__set($name, $value);
 127          }
 128      }
 129  
 130      /**
 131       * Method to attach a Form object to the field.
 132       *
 133       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 134       * @param   mixed              $value    The form field value to validate.
 135       * @param   string             $group    The field name group control value. This acts as an array container for the field.
 136       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 137       *                                       full field name would end up being "bar[foo]".
 138       *
 139       * @return  boolean  True on success.
 140       *
 141       * @see     FormField::setup()
 142       * @since   3.2
 143       */
 144      public function setup(\SimpleXMLElement $element, $value, $group = null)
 145      {
 146          $result = parent::setup($element, $value, $group);
 147  
 148          if ($result === true) {
 149              $this->menuType  = (string) $this->element['menu_type'];
 150              $this->clientId  = (int) $this->element['client_id'];
 151              $this->published = $this->element['published'] ? explode(',', (string) $this->element['published']) : array();
 152              $this->disable   = $this->element['disable'] ? explode(',', (string) $this->element['disable']) : array();
 153              $this->language  = $this->element['language'] ? explode(',', (string) $this->element['language']) : array();
 154          }
 155  
 156          return $result;
 157      }
 158  
 159      /**
 160       * Method to get the field option groups.
 161       *
 162       * @return  array  The field option objects as a nested array in groups.
 163       *
 164       * @since   1.6
 165       */
 166      protected function getGroups()
 167      {
 168          $groups = array();
 169  
 170          $menuType = $this->menuType;
 171  
 172          // Get the menu items.
 173          $items = MenusHelper::getMenuLinks($menuType, 0, 0, $this->published, $this->language, $this->clientId);
 174  
 175          // Build group for a specific menu type.
 176          if ($menuType) {
 177              // If the menutype is empty, group the items by menutype.
 178              $db    = $this->getDatabase();
 179              $query = $db->getQuery(true)
 180                  ->select($db->quoteName('title'))
 181                  ->from($db->quoteName('#__menu_types'))
 182                  ->where($db->quoteName('menutype') . ' = :menuType')
 183                  ->bind(':menuType', $menuType);
 184              $db->setQuery($query);
 185  
 186              try {
 187                  $menuTitle = $db->loadResult();
 188              } catch (\RuntimeException $e) {
 189                  $menuTitle = $menuType;
 190              }
 191  
 192              // Initialize the group.
 193              $groups[$menuTitle] = array();
 194  
 195              // Build the options array.
 196              foreach ($items as $link) {
 197                  $levelPrefix = str_repeat('- ', max(0, $link->level - 1));
 198  
 199                  // Displays language code if not set to All
 200                  if ($link->language !== '*') {
 201                      $lang = ' (' . $link->language . ')';
 202                  } else {
 203                      $lang = '';
 204                  }
 205  
 206                  $groups[$menuTitle][] = HTMLHelper::_(
 207                      'select.option',
 208                      $link->value,
 209                      $levelPrefix . $link->text . $lang,
 210                      'value',
 211                      'text',
 212                      \in_array($link->type, $this->disable)
 213                  );
 214              }
 215          } else {
 216              // Build groups for all menu types.
 217              // Build the groups arrays.
 218              foreach ($items as $menu) {
 219                  // Initialize the group.
 220                  $groups[$menu->title] = array();
 221  
 222                  // Build the options array.
 223                  foreach ($menu->links as $link) {
 224                      $levelPrefix = str_repeat('- ', max(0, $link->level - 1));
 225  
 226                      // Displays language code if not set to All
 227                      if ($link->language !== '*') {
 228                          $lang = ' (' . $link->language . ')';
 229                      } else {
 230                          $lang = '';
 231                      }
 232  
 233                      $groups[$menu->title][] = HTMLHelper::_(
 234                          'select.option',
 235                          $link->value,
 236                          $levelPrefix . $link->text . $lang,
 237                          'value',
 238                          'text',
 239                          \in_array($link->type, $this->disable)
 240                      );
 241                  }
 242              }
 243          }
 244  
 245          // Merge any additional groups in the XML definition.
 246          $groups = array_merge(parent::getGroups(), $groups);
 247  
 248          return $groups;
 249      }
 250  }


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