YoastSeoFormatter.php in Real-time SEO for Drupal 8.2
File
src/Plugin/Field/FieldFormatter/YoastSeoFormatter.php
View source
<?php
namespace Drupal\yoast_seo\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
class YoastSeoFormatter extends FormatterBase {
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);
$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;
}
}