You are here

public function BackgroundImageFormatter::renderElements in Simple Background image formatter 8

Render image elements.

Parameters

int $delta: If multifield number of element.

array $theme: Theme render array.

string $id: Field id.

Return value

array|null Rendered array of elements.

1 call to BackgroundImageFormatter::renderElements()
BackgroundImageFormatter::viewElements in src/Plugin/Field/FieldFormatter/BackgroundImageFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/BackgroundImageFormatter.php, line 186

Class

BackgroundImageFormatter
Plugin implementation of the background_image_formatter.

Namespace

Drupal\background_image_formatter\Plugin\Field\FieldFormatter

Code

public function renderElements($delta, array $theme, $id) {
  $elements = NULL;
  switch ($this
    ->getSetting('background_image_output_type')) {
    case 'css':
      $data = [
        '#tag' => 'style',
        '#value' => $this
          ->generateCssString($theme),
      ];
      $elements['#attached']['html_head'][] = [
        $data,
        'background_image_formatter_' . $id,
      ];
      break;
    case 'inline':
      $theme['#theme'] = 'background_image_formatter_inline';
      $elements[$delta] = [
        '#markup' => \Drupal::service('renderer')
          ->render($theme),
      ];
      break;
  }
  return $elements;
}