You are here

public function BlazyEntityBase::buildElements in Blazy 7

Returns media contents.

File

src/Plugin/Field/FieldFormatter/BlazyEntityBase.php, line 60

Class

BlazyEntityBase
Base class for entity reference formatters without field details.

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter

Code

public function buildElements(array &$build, $entities) {
  $settings =& $build['settings'];
  foreach ($entities as $delta => $entity) {

    // Overrides Constructor::targetType via blazy_entity_load().
    // Safe as a formatter is designed to work for a particular entity type
    // like what entityreference does, to not be confused with bundles.
    $this->targetType = $settings['target_type'] = $entity->targetType;
    list($entity_id) = entity_extract_ids($this->targetType, $entity);

    // Protect ourselves from recursive rendering.
    static $depth = 0;
    $depth++;
    if ($depth > 20) {
      throw new \Exception(t('Recursive rendering detected when rendering entity @entity_type(@entity_id). Aborting rendering.', [
        '@entity_type' => $settings['entity_type_id'],
        '@entity_id' => $entity_id,
      ]));
    }
    $settings['delta'] = $delta;
    $settings['entity_id'] = $entity_id;
    $this
      ->buildElement($build, $entity, $delta);
    $depth = 0;
  }

  // Supports Blazy formatter multi-breakpoint images if available.
  $settings['check_blazy'] = empty($settings['vanilla']);
}