[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2013 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\Factory;
  13  use Joomla\CMS\Language\Text;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Content Type field.
  21   *
  22   * @since  3.1
  23   */
  24  class ContenttypeField extends ListField
  25  {
  26      /**
  27       * A flexible tag list that respects access controls
  28       *
  29       * @var    string
  30       * @since  3.1
  31       */
  32      public $type = 'Contenttype';
  33  
  34      /**
  35       * Method to get the field input for a list of content types.
  36       *
  37       * @return  string  The field input.
  38       *
  39       * @since   3.1
  40       */
  41      protected function getInput()
  42      {
  43          if (!\is_array($this->value)) {
  44              if (\is_object($this->value)) {
  45                  $this->value = $this->value->tags;
  46              }
  47  
  48              if (\is_string($this->value)) {
  49                  $this->value = explode(',', $this->value);
  50              }
  51          }
  52  
  53          return parent::getInput();
  54      }
  55  
  56      /**
  57       * Method to get a list of content types
  58       *
  59       * @return  array  The field option objects.
  60       *
  61       * @since   3.1
  62       */
  63      protected function getOptions()
  64      {
  65          $lang = Factory::getLanguage();
  66          $db    = $this->getDatabase();
  67          $query = $db->getQuery(true)
  68              ->select(
  69                  [
  70                      $db->quoteName('a.type_id', 'value'),
  71                      $db->quoteName('a.type_title', 'text'),
  72                      $db->quoteName('a.type_alias', 'alias'),
  73                  ]
  74              )
  75              ->from($db->quoteName('#__content_types', 'a'))
  76              ->order($db->quoteName('a.type_title') . ' ASC');
  77  
  78          // Get the options.
  79          $db->setQuery($query);
  80  
  81          try {
  82              $options = $db->loadObjectList();
  83          } catch (\RuntimeException $e) {
  84              return array();
  85          }
  86  
  87          foreach ($options as $option) {
  88              // Make up the string from the component sys.ini file
  89              $parts = explode('.', $option->alias);
  90              $comp = array_shift($parts);
  91  
  92              // Make sure the component sys.ini is loaded
  93              $lang->load($comp . '.sys', JPATH_ADMINISTRATOR)
  94              || $lang->load($comp . '.sys', JPATH_ADMINISTRATOR . '/components/' . $comp);
  95  
  96              $option->string = implode('_', $parts);
  97              $option->string = $comp . '_CONTENT_TYPE_' . $option->string;
  98  
  99              if ($lang->hasKey($option->string)) {
 100                  $option->text = Text::_($option->string);
 101              }
 102          }
 103  
 104          // Merge any additional options in the XML definition.
 105          $options = array_merge(parent::getOptions(), $options);
 106  
 107          return $options;
 108      }
 109  }


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