MultipleItemsFieldWithCacheDependencyTest.php in Extra Field 8.2
File
tests/extra_field_test/src/Plugin/ExtraField/Display/MultipleItemsFieldWithCacheDependencyTest.php
View source
<?php
namespace Drupal\extra_field_test\Plugin\ExtraField\Display;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\extra_field\Plugin\ExtraFieldDisplayFormattedBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MultipleItemsFieldWithCacheDependencyTest extends ExtraFieldDisplayFormattedBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $renderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('renderer'));
}
public function viewElements(ContentEntityInterface $entity) {
$build = [];
$another_node_type_nodes = $this->entityTypeManager
->getStorage('node')
->loadByProperties([
'type' => 'another_node_type',
]);
foreach ($another_node_type_nodes as $another_node) {
$build[] = [
'#markup' => $another_node
->label(),
];
$this->renderer
->addCacheableDependency($build, $another_node);
}
return $build;
}
public function getLabel() {
return $this
->t('Related pages');
}
public function getLabelDisplay() {
return 'inline';
}
}