You are here

public function YoastSeoFormatter::viewElements in Real-time SEO for Drupal 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/YoastSeoFormatter.php \Drupal\yoast_seo\Plugin\Field\FieldFormatter\YoastSeoFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/YoastSeoFormatter.php, line 24

Class

YoastSeoFormatter
Plugin implementation of the 'example_formatter' formatter.

Namespace

Drupal\yoast_seo\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $yoast_seo_manager = \Drupal::service('yoast_seo.manager');
  foreach ($items as $delta => $item) {
    $status = $yoast_seo_manager
      ->getScoreStatus($item->status);

    // @todo find a way to give a weight, so the column doesn't appear
    // at the end.
    // Get template for the snippet.
    $overall_score_tpl = [
      '#theme' => 'view_overall_score',
      '#overall_score' => $status,
      '#attached' => [
        'library' => [
          'yoast_seo/yoast_seo_view',
        ],
      ],
    ];
    $output = \Drupal::service('renderer')
      ->render($overall_score_tpl);
    $elements[$delta] = [
      '#markup' => $output,
    ];
  }
  return $elements;
}