localgov_directories-3.3.1/src/Plugin/EntityReferenceSelection/LocalgovDirectoriesEntryTypes.php
src/Plugin/EntityReferenceSelection/LocalgovDirectoriesEntryTypes.php
<?php
namespace Drupal\localgov_directories\Plugin\EntityReferenceSelection;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginBase;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin description.
*
* @EntityReferenceSelection(
* id = "localgov_directories_entry_types",
* label = @Translation("Directories Entry Types"),
* group = "localgov_directories_entry_types",
* entity_types = {"node_type"},
* weight = 0
* )
*/
class LocalgovDirectoriesEntryTypes extends SelectionPluginBase implements ContainerFactoryPluginInterface {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a new DefaultSelection object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
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('entity_field.manager'),
$container->get('entity.repository')
);
}
/**
* {@inheritdoc}
*/
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$options = [];
// Logic repeated in DirectoryExtraFieldDisplay::directoryEntryTypes().
$entities = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
foreach ($entities as $entity_id => $entity) {
$fields = $this->entityFieldManager->getFieldDefinitions('node', $entity_id);
if (isset($fields['localgov_directory_channels'])) {
$bundle = $entity->bundle();
$options[$bundle][$entity_id] = Html::escape($this->entityRepository->getTranslationFromContext($entity)->label());
}
}
return $options;
}
/**
* {@inheritdoc}
*/
public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {
$options = $this->getReferenceableEntities($match, $match_operator);
return count($options);
}
/**
* {@inheritdoc}
*/
public function validateReferenceableEntities(array $ids) {
// Return only the $ids in $options, any others will be used in validation
// to list as invalid.
// @see Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete()
$options = $this->getReferenceableEntities();
return array_intersect($ids, array_keys($options['node_type']));
}
}
