[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/layouts/joomla/form/field/ -> media.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Admin
   5   * @subpackage  Layout
   6   *
   7   * @copyright   (C) 2015 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  defined('_JEXEC') or die;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Helper\MediaHelper;
  16  use Joomla\CMS\HTML\HTMLHelper;
  17  use Joomla\CMS\Language\Text;
  18  use Joomla\CMS\Router\Route;
  19  use Joomla\CMS\Uri\Uri;
  20  
  21  extract($displayData);
  22  
  23  /**
  24   * Layout variables
  25   * -----------------
  26   * @var  string   $asset           The asset text
  27   * @var  string   $authorField     The label text
  28   * @var  integer  $authorId        The author id
  29   * @var  string   $class           The class text
  30   * @var  boolean  $disabled        True if field is disabled
  31   * @var  string   $folder          The folder text
  32   * @var  string   $id              The label text
  33   * @var  string   $link            The link text
  34   * @var  string   $name            The name text
  35   * @var  string   $preview         The preview image relative path
  36   * @var  integer  $previewHeight   The image preview height
  37   * @var  integer  $previewWidth    The image preview width
  38   * @var  string   $onchange        The onchange text
  39   * @var  boolean  $readonly        True if field is readonly
  40   * @var  integer  $size            The size text
  41   * @var  string   $value           The value text
  42   * @var  string   $src             The path and filename of the image
  43   * @var  array    $mediaTypes      The supported media types for the Media Manager
  44   * @var  array    $imagesExt       The supported extensions for images
  45   * @var  array    $audiosExt       The supported extensions for audios
  46   * @var  array    $videosExt       The supported extensions for videos
  47   * @var  array    $documentsExt    The supported extensions for documents
  48   * @var  string   $dataAttribute   Miscellaneous data attributes preprocessed for HTML output
  49   * @var  array    $dataAttributes  Miscellaneous data attribute for eg, data-*
  50   */
  51  
  52  $attr = '';
  53  
  54  // Initialize some field attributes.
  55  $attr .= !empty($class) ? ' class="form-control field-media-input ' . $class . '"' : ' class="form-control field-media-input"';
  56  $attr .= !empty($size) ? ' size="' . $size . '"' : '';
  57  $attr .= $dataAttribute;
  58  
  59  // Initialize JavaScript field attributes.
  60  $attr .= !empty($onchange) ? ' onchange="' . $onchange . '"' : '';
  61  
  62  switch ($preview) {
  63      case 'no': // Deprecated parameter value
  64      case 'false':
  65      case 'none':
  66          $showPreview = false;
  67          break;
  68      case 'yes': // Deprecated parameter value
  69      case 'true':
  70      case 'show':
  71      case 'tooltip':
  72      default:
  73          $showPreview = true;
  74          break;
  75  }
  76  
  77  // Pre fill the contents of the popover
  78  if ($showPreview) {
  79      $cleanValue = MediaHelper::getCleanMediaFieldValue($value);
  80  
  81      if ($cleanValue && file_exists(JPATH_ROOT . '/' . $cleanValue)) {
  82          $src = Uri::root() . $value;
  83      } else {
  84          $src = '';
  85      }
  86  
  87      $width = $previewWidth;
  88      $height = $previewHeight;
  89      $style = '';
  90      $style .= ($width > 0) ? 'max-width:' . $width . 'px;' : '';
  91      $style .= ($height > 0) ? 'max-height:' . $height . 'px;' : '';
  92  
  93      $imgattr = array(
  94          'id' => $id . '_preview',
  95          'class' => 'media-preview',
  96          'style' => $style,
  97      );
  98  
  99      $img = HTMLHelper::_('image', $src, Text::_('JLIB_FORM_MEDIA_PREVIEW_ALT'), $imgattr);
 100  
 101      $previewImg = '<div id="' . $id . '_preview_img">' . $img . '</div>';
 102      $previewImgEmpty = '<div id="' . $id . '_preview_empty"' . ($src ? ' class="hidden"' : '') . '>'
 103          . Text::_('JLIB_FORM_MEDIA_PREVIEW_EMPTY') . '</div>';
 104  
 105      $showPreview = 'static';
 106  }
 107  
 108  // The url for the modal
 109  $url = ($readonly ? ''
 110      : ($link ?: 'index.php?option=com_media&view=media&tmpl=component&mediatypes=' . $mediaTypes
 111          . '&asset=' . $asset . '&author=' . $authorId)
 112      . '&fieldid={field-media-id}&path=' . $folder);
 113  
 114  // Correctly route the url to ensure it's correctly using sef modes and subfolders
 115  $url = Route::_($url);
 116  $doc = Factory::getDocument();
 117  $wam = $doc->getWebAssetManager();
 118  
 119  $wam->useScript('webcomponent.media-select');
 120  
 121  Text::script('JFIELD_MEDIA_LAZY_LABEL');
 122  Text::script('JFIELD_MEDIA_ALT_LABEL');
 123  Text::script('JFIELD_MEDIA_ALT_CHECK_LABEL');
 124  Text::script('JFIELD_MEDIA_ALT_CHECK_DESC_LABEL');
 125  Text::script('JFIELD_MEDIA_CLASS_LABEL');
 126  Text::script('JFIELD_MEDIA_FIGURE_CLASS_LABEL');
 127  Text::script('JFIELD_MEDIA_FIGURE_CAPTION_LABEL');
 128  Text::script('JFIELD_MEDIA_LAZY_LABEL');
 129  Text::script('JFIELD_MEDIA_SUMMARY_LABEL');
 130  Text::script('JFIELD_MEDIA_EMBED_CHECK_DESC_LABEL');
 131  Text::script('JFIELD_MEDIA_DOWNLOAD_CHECK_DESC_LABEL');
 132  Text::script('JFIELD_MEDIA_DOWNLOAD_CHECK_LABEL');
 133  Text::script('JFIELD_MEDIA_EMBED_CHECK_LABEL');
 134  Text::script('JFIELD_MEDIA_WIDTH_LABEL');
 135  Text::script('JFIELD_MEDIA_TITLE_LABEL');
 136  Text::script('JFIELD_MEDIA_HEIGHT_LABEL');
 137  Text::script('JFIELD_MEDIA_UNSUPPORTED');
 138  Text::script('JFIELD_MEDIA_DOWNLOAD_FILE');
 139  Text::script('JLIB_APPLICATION_ERROR_SERVER');
 140  Text::script('JLIB_FORM_MEDIA_PREVIEW_EMPTY', true);
 141  
 142  $modalHTML = HTMLHelper::_(
 143      'bootstrap.renderModal',
 144      'imageModal_' . $id,
 145      [
 146          'url'         => $url,
 147          'title'       => Text::_('JLIB_FORM_CHANGE_IMAGE'),
 148          'closeButton' => true,
 149          'height'      => '100%',
 150          'width'       => '100%',
 151          'modalWidth'  => '80',
 152          'bodyHeight'  => '60',
 153          'footer'      => '<button type="button" class="btn btn-success button-save-selected">' . Text::_('JSELECT') . '</button>'
 154              . '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">' . Text::_('JCANCEL') . '</button>',
 155      ]
 156  );
 157  
 158  $wam->useStyle('webcomponent.field-media')
 159      ->useScript('webcomponent.field-media');
 160  
 161  if (count($doc->getScriptOptions('media-picker')) === 0) {
 162      $doc->addScriptOptions('media-picker', [
 163          'images'    => $imagesExt,
 164          'audios'    => $audiosExt,
 165          'videos'    => $videosExt,
 166          'documents' => $documentsExt,
 167      ]);
 168  }
 169  
 170  ?>
 171  <joomla-field-media class="field-media-wrapper" type="image" <?php // @TODO add this attribute to the field in order to use it for all media types ?> 
 172      base-path="<?php echo Uri::root(); ?>" 
 173      root-folder="<?php echo ComponentHelper::getParams('com_media')->get('file_path', 'images'); ?>" 
 174      url="<?php echo $url; ?>" 
 175      modal-container=".modal" 
 176      modal-width="100%" 
 177      modal-height="400px" 
 178      input=".field-media-input" 
 179      button-select=".button-select" 
 180      button-clear=".button-clear" 
 181      button-save-selected=".button-save-selected"
 182      preview="static" 
 183      preview-container=".field-media-preview" 
 184      preview-width="<?php echo $previewWidth; ?>" 
 185      preview-height="<?php echo $previewHeight; ?>" 
 186      supported-extensions="<?php echo str_replace('"', '&quot;', json_encode(['images' => $imagesAllowedExt, 'audios' => $audiosAllowedExt, 'videos' => $videosAllowedExt, 'documents' => $documentsAllowedExt])); ?>
 187  ">
 188      <?php echo $modalHTML; ?>
 189      <?php if ($showPreview) : ?>
 190          <div class="field-media-preview">
 191              <?php echo ' ' . $previewImgEmpty; ?>
 192              <?php echo ' ' . $previewImg; ?>
 193          </div>
 194      <?php endif; ?>
 195      <div class="input-group">
 196          <input type="text" name="<?php echo $name; ?>" id="<?php echo $id; ?>" value="<?php echo htmlspecialchars($value, ENT_COMPAT, 'UTF-8'); ?>" <?php echo $attr; ?>>
 197          <?php if ($disabled != true) : ?>
 198              <button type="button" class="btn btn-success button-select"><?php echo Text::_('JLIB_FORM_BUTTON_SELECT'); ?></button>
 199              <button type="button" class="btn btn-danger button-clear"><span class="icon-times" aria-hidden="true"></span><span class="visually-hidden"><?php echo Text::_('JLIB_FORM_BUTTON_CLEAR'); ?></span></button>
 200          <?php endif; ?>
 201      </div>
 202  </joomla-field-media>


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