[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2020 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  // phpcs:disable PSR1.Files.SideEffects
  13  \defined('JPATH_PLATFORM') or die;
  14  // phpcs:enable PSR1.Files.SideEffects
  15  
  16  /**
  17   * The Field to load the form inside current form
  18   *
  19   * @since  4.0.0
  20   */
  21  class AccessiblemediaField extends SubformField
  22  {
  23      /**
  24       * The form field type.
  25       *
  26       * @var    string
  27       * @since  4.0.0
  28       */
  29      protected $type = 'Accessiblemedia';
  30  
  31      /**
  32       * The preview.
  33       *
  34       * @var    string
  35       * @since  4.0.0
  36       */
  37      protected $preview;
  38  
  39      /**
  40       * The directory.
  41       *
  42       * @var    string
  43       * @since  4.0.0
  44       */
  45      protected $directory;
  46  
  47      /**
  48       * The previewWidth.
  49       *
  50       * @var    integer
  51       * @since  4.0.0
  52       */
  53      protected $previewWidth;
  54  
  55      /**
  56       * The previewHeight.
  57       *
  58       * @var    integer
  59       * @since  4.0.0
  60       */
  61      protected $previewHeight;
  62  
  63      /**
  64       * Layout to render
  65       *
  66       * @var    string
  67       * @since  4.0.0
  68       */
  69      protected $layout;
  70  
  71      /**
  72       * Method to get certain otherwise inaccessible properties from the form field object.
  73       *
  74       * @param   string  $name  The property name for which to get the value.
  75       *
  76       * @return  mixed  The property value or null.
  77       *
  78       * @since   4.0.0
  79       */
  80      public function __get($name)
  81      {
  82          switch ($name) {
  83              case 'directory':
  84              case 'preview':
  85              case 'previewHeight':
  86              case 'previewWidth':
  87                  return $this->$name;
  88          }
  89  
  90          return parent::__get($name);
  91      }
  92  
  93      /**
  94       * Method to set certain otherwise inaccessible properties of the form field object.
  95       *
  96       * @param   string  $name   The property name for which to set the value.
  97       * @param   mixed   $value  The value of the property.
  98       *
  99       * @return  void
 100       *
 101       * @since   4.0.0
 102       */
 103      public function __set($name, $value)
 104      {
 105          switch ($name) {
 106              case 'directory':
 107              case 'preview':
 108                  $this->$name = (string) $value;
 109                  break;
 110  
 111              case 'previewHeight':
 112              case 'previewWidth':
 113                  $this->$name = (int) $value;
 114                  break;
 115  
 116              default:
 117                  parent::__set($name, $value);
 118          }
 119      }
 120  
 121      /**
 122       * Method to attach a Form object to the field.
 123       *
 124       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the <field /> tag for the form field object.
 125       * @param   mixed              $value    The form field value to validate.
 126       * @param   string             $group    The field name group control value.
 127       *
 128       * @return  boolean  True on success.
 129       *
 130       * @since   4.0.0
 131       */
 132      public function setup(\SimpleXMLElement $element, $value, $group = null)
 133      {
 134          /**
 135           * When you have subforms which are not repeatable (i.e. a subform custom field with the
 136           * repeat attribute set to 0) you get an array here since the data comes from decoding the
 137           * JSON into an associative array, including the media subfield's data.
 138           *
 139           * However, this method expects an object or a string, not an array. Typecasting the array
 140           * to an object solves the data format discrepancy.
 141           */
 142          $value = is_array($value) ? (object) $value : $value;
 143  
 144          /**
 145           * If the value is not a string, it is
 146           * most likely within a custom field of type subform
 147           * and the value is a stdClass with properties
 148           * imagefile and alt_text. So it is fine.
 149          */
 150          if (\is_string($value)) {
 151              json_decode($value);
 152  
 153              // Check if value is a valid JSON string.
 154              if ($value !== '' && json_last_error() !== JSON_ERROR_NONE) {
 155                  /**
 156                   * If the value is not empty and is not a valid JSON string,
 157                   * it is most likely a custom field created in Joomla 3 and
 158                   * the value is a string that contains the file name.
 159                  */
 160                  if (is_file(JPATH_ROOT . '/' . $value)) {
 161                      $value = '{"imagefile":"' . $value . '","alt_text":""}';
 162                  } else {
 163                      $value = '';
 164                  }
 165              }
 166          } elseif (
 167              !is_object($value)
 168              || !property_exists($value, 'imagefile')
 169              || !property_exists($value, 'alt_text')
 170          ) {
 171              return false;
 172          }
 173  
 174          if (!parent::setup($element, $value, $group)) {
 175              return false;
 176          }
 177  
 178          $this->directory = (string) $this->element['directory'];
 179          $this->preview = (string) $this->element['preview'];
 180          $this->previewHeight = isset($this->element['preview_height']) ? (int) $this->element['preview_height'] : 200;
 181          $this->previewWidth = isset($this->element['preview_width']) ? (int) $this->element['preview_width'] : 200;
 182  
 183          $xml = <<<XML
 184  <?xml version="1.0" encoding="utf-8"?>
 185  <form>
 186      <fieldset
 187          name="accessiblemedia"
 188          label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_LABEL"
 189      >
 190          <field
 191              name="imagefile"
 192              type="media"
 193              label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_IMAGEFILE_LABEL"
 194              directory="$this->directory"
 195              preview="$this->preview"
 196              preview_width="$this->previewWidth"
 197              preview_height="$this->previewHeight"
 198          />
 199  
 200          <field
 201              name="alt_text"
 202              type="text"
 203              label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_TEXT_LABEL"
 204          />
 205  
 206          <field
 207              name="alt_empty"
 208              type="checkbox"
 209              label="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_LABEL"
 210              description="JLIB_FORM_FIELD_PARAM_ACCESSIBLEMEDIA_PARAMS_ALT_EMPTY_DESC"
 211          />
 212      </fieldset>
 213  </form>
 214  XML;
 215          $this->formsource = $xml;
 216  
 217          $this->layout = 'joomla.form.field.media.accessiblemedia';
 218  
 219          return true;
 220      }
 221  }


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