You are here

abstract class BlazyAdminFormatterBase in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/Form/BlazyAdminFormatterBase.php \Drupal\blazy\Form\BlazyAdminFormatterBase
  2. 7 src/Form/BlazyAdminFormatterBase.php \Drupal\blazy\Form\BlazyAdminFormatterBase

A base for field formatter admin to have re-usable methods in one place.

Hierarchy

Expanded class hierarchy of BlazyAdminFormatterBase

1 file declares its use of BlazyAdminFormatterBase
BlazyAdminExtended.php in src/Dejavu/BlazyAdminExtended.php

File

src/Form/BlazyAdminFormatterBase.php, line 11

Namespace

Drupal\blazy\Form
View source
abstract class BlazyAdminFormatterBase extends BlazyAdminBase {

  /**
   * Returns re-usable image formatter form elements.
   */
  public function imageStyleForm(array &$form, $definition = []) {
    $is_responsive = function_exists('responsive_image_get_image_dimensions');
    if (empty($definition['no_image_style'])) {
      $form['image_style'] = $this
        ->baseForm($definition)['image_style'];
    }
    if (!empty($definition['thumbnail_style'])) {
      $form['thumbnail_style'] = $this
        ->baseForm($definition)['thumbnail_style'];
    }
    if ($is_responsive && !empty($definition['responsive_image'])) {
      $url = Url::fromRoute('entity.responsive_image_style.collection')
        ->toString();
      $form['responsive_image_style'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Responsive image'),
        '#options' => $this
          ->getResponsiveImageOptions(),
        '#description' => $this
          ->t('Responsive image style for the main stage image is more reasonable for large images. Works with multi-serving IMG, or PICTURE element. Not compatible with breakpoints and aspect ratio, yet. Leave empty to disable. <a href=":url" target="_blank">Manage responsive image styles</a>.', [
          ':url' => $url,
        ]),
        '#access' => $this
          ->getResponsiveImageOptions(),
        '#weight' => -100,
      ];
      if (!empty($definition['background'])) {
        $form['background']['#states'] = $this
          ->getState(static::STATE_RESPONSIVE_IMAGE_STYLE_DISABLED, $definition);
      }
    }
    if (!empty($definition['thumbnail_effect'])) {
      $form['thumbnail_effect'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Thumbnail effect'),
        '#options' => isset($definition['thumbnail_effect']) ? $definition['thumbnail_effect'] : [],
        '#weight' => -100,
      ];
    }
  }

  /**
   * Return the field formatter settings summary.
   *
   * @deprecated in blazy:8.x-1.0 and is removed from blazy:8.x-2.0. Use
   *   self::getSettingsSummary() instead.
   * @see https://www.drupal.org/node/3103018
   */
  public function settingsSummary($plugin, $definition = []) {
    $definition = isset($definition) ? $definition : $plugin
      ->getScopedFormElements();
    $definition['settings'] = isset($definition['settings']) ? $definition['settings'] : $plugin
      ->getSettings();
    return $this
      ->getSettingsSummary($definition);
  }

  /**
   * Return the field formatter settings summary.
   */
  public function getSettingsSummary($definition = []) {
    $summary = [];
    if (empty($definition['settings'])) {
      return $summary;
    }
    $this
      ->getExcludedSettingsSummary($definition);
    $enforced = [
      'optionset',
      'cache',
      'skin',
      'view_mode',
      'override',
      'overridables',
      'style',
      'vanilla',
    ];
    $enforced = isset($definition['enforced']) ? $definition['enforced'] : $enforced;
    $settings = array_filter($definition['settings']);
    $breakpoints = isset($settings['breakpoints']) && is_array($settings['breakpoints']) ? array_filter($settings['breakpoints']) : [];
    foreach ($definition['settings'] as $key => $setting) {
      $title = Unicode::ucfirst(str_replace('_', ' ', $key));
      $vanilla = !empty($settings['vanilla']);
      if ($key == 'breakpoints') {
        $widths = [];
        if ($breakpoints) {
          foreach ($breakpoints as $breakpoint) {
            if (!empty($breakpoint['width'])) {
              $widths[] = $breakpoint['width'];
            }
          }
        }
        $title = 'Breakpoints';
        $setting = $widths ? implode(', ', $widths) : 'none';
      }
      else {
        if ($vanilla && !in_array($key, $enforced)) {
          continue;
        }
        if ($key == 'override' && empty($setting)) {
          unset($settings['overridables']);
        }
        if (is_bool($setting) && $setting) {
          $setting = 'yes';
        }
        elseif (is_array($setting)) {
          $setting = array_filter($setting);
          if (!empty($setting)) {
            $setting = implode(', ', $setting);
          }
        }
        if ($key == 'cache') {
          $setting = $this
            ->getCacheOptions()[$setting];
        }
      }
      if (empty($setting)) {
        continue;
      }
      if (isset($settings[$key]) && is_string($setting)) {
        $summary[] = $this
          ->t('@title: <strong>@setting</strong>', [
          '@title' => $title,
          '@setting' => $setting,
        ]);
      }
    }
    return $summary;
  }

  /**
   * Exclude the field formatter settings summary as required.
   */
  public function getExcludedSettingsSummary(array &$definition = []) {
    $settings =& $definition['settings'];
    $excludes = empty($definition['excludes']) ? [] : $definition['excludes'];
    $plugin_id = isset($definition['plugin_id']) ? $definition['plugin_id'] : '';
    $blazy = $plugin_id && strpos($plugin_id, 'blazy') !== FALSE;
    $image_styles = function_exists('image_style_options') ? image_style_options(TRUE) : [];
    $media_switch = empty($settings['media_switch']) ? '' : $settings['media_switch'];
    unset($image_styles['']);
    $excludes['current_view_mode'] = TRUE;
    if ($blazy) {
      $excludes['optionset'] = TRUE;
    }
    if ($media_switch != 'media') {
      $excludes['iframe_lazy'] = TRUE;
    }
    if (!empty($settings['responsive_image_style'])) {
      foreach ([
        'ratio',
        'breakpoints',
        'background',
        'sizes',
      ] as $key) {
        $excludes[$key] = TRUE;
      }
    }
    if (empty($settings['grid'])) {
      foreach ([
        'grid',
        'grid_medium',
        'grid_small',
        'visible_items',
      ] as $key) {
        $excludes[$key] = TRUE;
      }
    }

    // Remove exluded settings.
    foreach ($excludes as $key => $value) {
      if (isset($settings[$key])) {
        unset($settings[$key]);
      }
    }
    foreach ($settings as $key => $setting) {
      if ($key == 'style' || $key == 'responsive_image_style' || empty($settings[$key])) {
        continue;
      }
      if (strpos($key, 'style') !== FALSE && isset($image_styles[$settings[$key]])) {
        $settings[$key] = $image_styles[$settings[$key]];
      }
    }
  }

  /**
   * Returns available fields for select options.
   */
  public function getFieldOptions($target_bundles = [], $allowed_field_types = [], $entity_type = 'media', $target_type = '') {
    $options = [];
    $storage = $this
      ->blazyManager()
      ->getEntityTypeManager()
      ->getStorage('field_config');

    // Fix for Views UI not recognizing Media bundles, unlike Formatters.
    if (empty($target_bundles)) {
      $bundle_service = \Drupal::service('entity_type.bundle.info');
      $target_bundles = $bundle_service
        ->getBundleInfo($entity_type);
    }

    // Declutters options from less relevant options.
    $excludes = $this
      ->getExcludedFieldOptions();
    foreach ($target_bundles as $bundle => $label) {
      if ($fields = $storage
        ->loadByProperties([
        'entity_type' => $entity_type,
        'bundle' => $bundle,
      ])) {
        foreach ((array) $fields as $field) {
          if (in_array($field
            ->getName(), $excludes)) {
            continue;
          }
          if (empty($allowed_field_types)) {
            $options[$field
              ->getName()] = $field
              ->getLabel();
          }
          elseif (in_array($field
            ->getType(), $allowed_field_types)) {
            $options[$field
              ->getName()] = $field
              ->getLabel();
          }
          if (!empty($target_type) && $field
            ->getSetting('target_type') == $target_type) {
            $options[$field
              ->getName()] = $field
              ->getLabel();
          }
        }
      }
    }
    return $options;
  }

  /**
   * Declutters options from less relevant options.
   */
  public function getExcludedFieldOptions() {
    $excludes = 'field_document_size field_id field_media_in_library field_mime_type field_source field_tweet_author field_tweet_id field_tweet_url field_media_video_embed_field field_instagram_shortcode field_instagram_url';
    $excludes = explode(' ', $excludes);
    $excludes = array_combine($excludes, $excludes);
    $this->blazyManager
      ->getModuleHandler()
      ->alter('blazy_excluded_field_options', $excludes);
    return $excludes;
  }

  /**
   * Returns Responsive image for select options.
   */
  public function getResponsiveImageOptions() {
    $options = [];
    if ($this
      ->blazyManager()
      ->getModuleHandler()
      ->moduleExists('responsive_image')) {
      $image_styles = $this
        ->blazyManager()
        ->entityLoadMultiple('responsive_image_style');
      if (!empty($image_styles)) {
        foreach ($image_styles as $name => $image_style) {
          if ($image_style
            ->hasImageStyleMappings()) {
            $options[$name] = strip_tags($image_style
              ->label());
          }
        }
      }
    }
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyAdminBase::$blazyManager protected property The blazy manager service.
BlazyAdminBase::$entityDisplayRepository protected property The entity type manager service.
BlazyAdminBase::$typedConfig protected property The typed config manager service.
BlazyAdminBase::baseForm public function Returns simple form elements common for Views field, EB widget, formatters.
BlazyAdminBase::blazyManager public function Returns the blazy manager.
BlazyAdminBase::breakpointElements public function Defines re-usable breakpoints form.
BlazyAdminBase::breakpointsForm public function Defines re-usable breakpoints form.
BlazyAdminBase::closingForm public function Returns shared ending form elements across field formatter and Views. 1
BlazyAdminBase::create public static function
BlazyAdminBase::finalizeForm public function Returns re-usable logic, styling and assets across fields and Views.
BlazyAdminBase::getCacheOptions public function Returns time in interval for select options.
BlazyAdminBase::getEntityDisplayRepository public function Returns the entity display repository.
BlazyAdminBase::getOptionsetOptions public function Returns available optionsets for select options.
BlazyAdminBase::getState protected function Get one of the pre-defined states used in this form.
BlazyAdminBase::getTypedConfig public function Returns the typed config.
BlazyAdminBase::getViewModeOptions public function Returns available view modes for select options.
BlazyAdminBase::gridForm public function Returns re-usable grid elements across field formatter and Views.
BlazyAdminBase::mediaSwitchForm public function Returns re-usable media switch form elements.
BlazyAdminBase::openingForm public function Returns shared form elements across field formatter and Views. 1
BlazyAdminBase::STATE_IFRAME_ENABLED constant A state that represents the media switch iframe is enabled.
BlazyAdminBase::STATE_IMAGE_RENDERED_ENABLED constant A state that represents the image rendered switch is enabled.
BlazyAdminBase::STATE_LIGHTBOX_CUSTOM constant A state that represents the custom lightbox caption is enabled.
BlazyAdminBase::STATE_LIGHTBOX_ENABLED constant A state that represents the media switch lightbox is enabled.
BlazyAdminBase::STATE_RESPONSIVE_IMAGE_STYLE_DISABLED constant A state that represents the responsive image style is disabled.
BlazyAdminBase::STATE_THUMBNAIL_STYLE_ENABLED constant A state that represents the thumbnail style is enabled.
BlazyAdminBase::__construct public function Constructs a BlazyAdminBase object.
BlazyAdminFormatterBase::getExcludedFieldOptions public function Declutters options from less relevant options.
BlazyAdminFormatterBase::getExcludedSettingsSummary public function Exclude the field formatter settings summary as required.
BlazyAdminFormatterBase::getFieldOptions public function Returns available fields for select options.
BlazyAdminFormatterBase::getResponsiveImageOptions public function Returns Responsive image for select options.
BlazyAdminFormatterBase::getSettingsSummary public function Return the field formatter settings summary.
BlazyAdminFormatterBase::imageStyleForm public function Returns re-usable image formatter form elements.
BlazyAdminFormatterBase::settingsSummary Deprecated public function Return the field formatter settings summary.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.