You are here

class BlazyFormatterManager in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/BlazyFormatterManager.php \Drupal\blazy\BlazyFormatterManager

Provides common field formatter-related methods: Blazy, Slick.

Hierarchy

Expanded class hierarchy of BlazyFormatterManager

3 files declare their use of BlazyFormatterManager
BlazyFileFormatterBase.php in src/Plugin/Field/FieldFormatter/BlazyFileFormatterBase.php
BlazyFormatterTest.php in tests/modules/blazy_test/src/BlazyFormatterTest.php
BlazyVideoFormatter.php in src/Plugin/Field/FieldFormatter/BlazyVideoFormatter.php
1 string reference to 'BlazyFormatterManager'
blazy.services.yml in ./blazy.services.yml
blazy.services.yml
1 service uses BlazyFormatterManager
blazy.formatter.manager in ./blazy.services.yml
Drupal\blazy\BlazyFormatterManager

File

src/BlazyFormatterManager.php, line 8

Namespace

Drupal\blazy
View source
class BlazyFormatterManager extends BlazyManager {

  /**
   * The first image item found.
   *
   * @var object
   */
  protected $firstItem = NULL;

  /**
   * Returns the field formatter settings inherited by child elements.
   *
   * @param array $build
   *   The array containing: settings, or potential optionset for extensions.
   * @param object $items
   *   The items to prepare settings for.
   */
  public function buildSettings(array &$build, $items) {
    $settings =& $build['settings'];
    $settings += $this
      ->getCommonSettings();
    $count = $items
      ->count();
    $field = $items
      ->getFieldDefinition();
    $entity = $items
      ->getEntity();
    $entity_type_id = $entity
      ->getEntityTypeId();
    $entity_id = $entity
      ->id();
    $bundle = $entity
      ->bundle();
    $field_name = $field
      ->getName();
    $field_type = $field
      ->getType();
    $field_clean = str_replace("field_", '', $field_name);
    $target_type = $field
      ->getFieldStorageDefinition()
      ->getSetting('target_type');
    $view_mode = empty($settings['current_view_mode']) ? '_custom' : $settings['current_view_mode'];
    $namespace = $settings['namespace'] = empty($settings['namespace']) ? 'blazy' : $settings['namespace'];
    $id = isset($settings['id']) ? $settings['id'] : '';
    $id = Blazy::getHtmlId("{$namespace}-{$entity_type_id}-{$entity_id}-{$field_clean}-{$view_mode}", $id);
    $switch = empty($settings['media_switch']) ? '' : $settings['media_switch'];
    $internal_path = $absolute_path = NULL;

    // Deals with UndefinedLinkTemplateException such as paragraphs type.
    // @see #2596385, or fetch the host entity.
    if (!$entity
      ->isNew() && method_exists($entity, 'hasLinkTemplate')) {
      if ($entity
        ->hasLinkTemplate('canonical')) {
        $url = $entity
          ->toUrl();
        $internal_path = $url
          ->getInternalPath();
        $absolute_path = $url
          ->setAbsolute()
          ->toString();
      }
    }
    $settings['bundle'] = $bundle;
    $settings['cache_metadata'] = [
      'keys' => [
        $id,
        $count,
      ],
    ];
    $settings['cache_tags'][] = $entity_type_id . ':' . $entity_id;
    $settings['content_url'] = $settings['absolute_path'] = $absolute_path;
    $settings['count'] = $count;
    $settings['entity_id'] = $entity_id;
    $settings['entity_type_id'] = $entity_type_id;
    $settings['field_type'] = $field_type;
    $settings['field_name'] = $field_name;
    $settings['id'] = $id;
    $settings['internal_path'] = $internal_path;
    $settings['lightbox'] = $switch && in_array($switch, $this
      ->getLightboxes()) ? $switch : FALSE;
    $settings['target_type'] = $target_type;

    // Bail out if vanilla is requested.
    if (!empty($settings['vanilla'])) {
      $settings = array_filter($settings);
      return;
    }
    $settings['breakpoints'] = isset($settings['breakpoints']) && empty($settings['responsive_image_style']) ? $settings['breakpoints'] : [];
    if (!empty($settings['breakpoints'])) {
      $this
        ->cleanUpBreakpoints($settings);
    }
    $settings['caption'] = empty($settings['caption']) ? [] : array_filter($settings['caption']);
    $settings['background'] = empty($settings['responsive_image_style']) && !empty($settings['background']);
    $settings['resimage'] = function_exists('responsive_image_get_image_dimensions') && !empty($settings['responsive_image']) && !empty($settings['responsive_image_style']);
    $settings['resimage'] = $settings['resimage'] ? $this
      ->entityLoad($settings['responsive_image_style'], 'responsive_image_style') : FALSE;
    $settings['blazy'] = !empty($settings['blazy']) || $settings['background'] || !empty($settings['resimage']) || !empty($settings['breakpoints']);
    $settings['lazy'] = $settings['blazy'] ? 'blazy' : (isset($settings['lazy']) ? $settings['lazy'] : '');

    // Aspect ratio isn't working with Responsive image, yet.
    // However allows custom work to get going with an enforced.
    $ratio = FALSE;
    if (!empty($settings['ratio'])) {
      $ratio = empty($settings['responsive_image_style']);
      if ($settings['ratio'] == 'enforced' || $settings['background']) {
        $ratio = TRUE;
      }
    }
    $settings['ratio'] = $ratio ? $settings['ratio'] : FALSE;

    // Sets dimensions once, if cropped, to reduce costs with ton of images.
    // This is less expensive than re-defining dimensions per image.
    if (!empty($settings['image_style']) && empty($settings['resimage'])) {
      if ($field_type == 'image' && ($item = $items[0])) {
        $settings['item'] = $item;
        $settings['uri'] = ($file = $item->entity) && empty($item->uri) ? $file
          ->getFileUri() : $item->uri;
      }
      if (!empty($settings['uri'])) {
        $this
          ->setDimensionsOnce($settings);
      }
    }
    $this
      ->getModuleHandler()
      ->alter($namespace . '_settings', $build, $items);
  }

  /**
   * Backported few cross-module methods to minimize mismatched branch issues.
   */
  public function preBuildElements(array &$build, $items, array $entities = []) {
    $this
      ->buildSettings($build, $items);
  }

  /**
   * Backported few cross-module methods to minimize mismatched branch issues.
   */
  public function postBuildElements(array &$build, $items, array $entities = []) {

    // Rebuild the first item to build colorbox/zoom-like gallery.
    $build['settings']['first_item'] = $this->firstItem;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyFormatterManager::$firstItem protected property The first image item found.
BlazyFormatterManager::buildSettings public function Returns the field formatter settings inherited by child elements.
BlazyFormatterManager::postBuildElements public function Backported few cross-module methods to minimize mismatched branch issues.
BlazyFormatterManager::preBuildElements public function Backported few cross-module methods to minimize mismatched branch issues.
BlazyManager::buildDataBlazy public function Builds breakpoints suitable for top-level [data-blazy] wrapper attributes.
BlazyManager::cleanUpBreakpoints public function Cleans up empty breakpoints.
BlazyManager::getBlazy public function Returns the enforced content, or image using theme_blazy().
BlazyManager::getEntityView public function Returns the entity view, if available.
BlazyManager::getImage public function Backported few cross-module methods to minimize mismatched branch issues.
BlazyManager::getResponsiveImageCacheTags public function Returns the Responsive image cache tags.
BlazyManager::isBlazy public function Checks for Blazy formatter such as from within a Views style plugin.
BlazyManager::isCrop public function Checks if an image style contains crop effect.
BlazyManager::preRenderImage public function Builds the Blazy image as a structured array ready for ::renderer().
BlazyManager::setDimensionsOnce public function Sets dimensions once to reduce method calls, if image style contains crop.
BlazyManagerBase::$cache protected property The cache backend.
BlazyManagerBase::$configFactory protected property The config factory.
BlazyManagerBase::$entityTypeManager protected property The entity type manager service.
BlazyManagerBase::$moduleHandler protected property The module handler service.
BlazyManagerBase::$renderer protected property The renderer.
BlazyManagerBase::attach public function Returns array of needed assets suitable for #attached property.
BlazyManagerBase::buildSkins public function Collects defined skins as registered via hook_MODULE_NAME_skins_info().
BlazyManagerBase::configLoad public function Returns any config, or keyed by the $setting_name.
BlazyManagerBase::create public static function
BlazyManagerBase::entityLoad public function Returns a shortcut for loading a config entity: image_style, slick, etc.
BlazyManagerBase::entityLoadMultiple public function Returns a shortcut for loading multiple configuration entities.
BlazyManagerBase::getCache public function Returns the cache.
BlazyManagerBase::getCacheMetadata public function Return the cache metadata common for all blazy-related modules.
BlazyManagerBase::getCommonSettings public function Returns the common settings inherited down to each item.
BlazyManagerBase::getConfigFactory public function Returns the config factory.
BlazyManagerBase::getEntityTypeManager public function Returns the entity type manager.
BlazyManagerBase::getLightboxes public function Gets the supported lightboxes.
BlazyManagerBase::getModuleHandler public function Returns the module handler.
BlazyManagerBase::getRenderer public function Returns the renderer.
BlazyManagerBase::__construct public function Constructs a BlazyManager object.