You are here

public function BlazyFormatter::buildSettings in Blazy 8.2

Same name and namespace in other branches
  1. 7 src/BlazyFormatter.php \Drupal\blazy\BlazyFormatter::buildSettings()

Modifies the field formatter settings inherited by child elements.

Parameters

array $build: The array containing: settings, or potential optionset for extensions.

object $items: The Drupal\Core\Field\FieldItemListInterface items.

Overrides BlazyFormatterInterface::buildSettings

1 call to BlazyFormatter::buildSettings()
BlazyFormatter::preBuildElements in src/BlazyFormatter.php
Modifies the field formatter settings inherited by child elements.

File

src/BlazyFormatter.php, line 34

Class

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

Namespace

Drupal\blazy

Code

public function buildSettings(array &$build, $items) {
  $settings =& $build['settings'];
  $this
    ->getCommonSettings($settings);
  $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_clean = str_replace("field_", '', $field_name);
  $view_mode = empty($settings['current_view_mode']) ? '_custom' : $settings['current_view_mode'];
  $namespace = $settings['namespace'];
  $id = isset($settings['id']) ? $settings['id'] : '';
  $gallery_id = "{$namespace}-{$entity_type_id}-{$bundle}-{$field_clean}-{$view_mode}";
  $id = Blazy::getHtmlId("{$gallery_id}-{$entity_id}", $id);
  $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')) {

      // Check if multilingual is enabled (@see #3214002).
      if ($entity
        ->hasTranslation($settings['current_language'])) {

        // Load the translated url.
        $url = $entity
          ->getTranslation($settings['current_language'])
          ->toUrl();
      }
      else {

        // Otherwise keep the standard url.
        $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['caption'] = empty($settings['caption']) ? [] : array_filter($settings['caption']);
  $settings['content_url'] = $settings['absolute_path'] = $absolute_path;
  $settings['count'] = $count;
  $settings['entity_id'] = $entity_id;
  $settings['entity_type_id'] = $entity_type_id;
  $settings['gallery_id'] = str_replace('_', '-', $gallery_id . '-' . $settings['media_switch']);
  $settings['id'] = $id;
  $settings['internal_path'] = $internal_path;
  $settings['use_field'] = !$settings['lightbox'] && isset($settings['third_party'], $settings['third_party']['linked_field']) && !empty($settings['third_party']['linked_field']['linked']);

  // Bail out if Vanilla mode is requested.
  if (!empty($settings['vanilla'])) {
    $settings = array_filter($settings);
    return;
  }

  // Lazy load types: blazy, and slick: ondemand, anticipated, progressive.
  $settings['blazy'] = !empty($settings['blazy']) || !empty($settings['background']) || $settings['resimage'];
  $settings['lazy'] = $settings['blazy'] ? 'blazy' : (isset($settings['lazy']) ? $settings['lazy'] : '');
  $settings['lazy'] = empty($settings['is_preview']) ? $settings['lazy'] : '';
}