class ActiveEntity in Configuration selector 8.2
Same name and namespace in other branches
- 8 src/ActiveEntity.php \Drupal\config_selector\ActiveEntity
Helper for getting the active entity or ID of any config_selector feature.
Hierarchy
- class \Drupal\config_selector\ActiveEntity uses ConfigSelectorSortTrait
Expanded class hierarchy of ActiveEntity
1 string reference to 'ActiveEntity'
1 service uses ActiveEntity
File
- src/
ActiveEntity.php, line 11
Namespace
Drupal\config_selectorView source
class ActiveEntity {
use ConfigSelectorSortTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* ActiveEntity constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* Gets the active entity from the entity type and config_selector feature.
*
* @param string $entity_type_id
* The entity type to get the ID for.
* @param string $feature
* The config selector feature to get the ID for.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The active entity for the provided entity type and feature. NULL is there
* is no corresponding entity.
*/
public function get($entity_type_id, $feature) {
$entity_storage = $this->entityTypeManager
->getStorage($entity_type_id);
$matching_config = $entity_storage
->getQuery()
->condition('third_party_settings.config_selector.feature', $feature)
->condition('status', FALSE, '<>')
->execute();
$config_entities = $entity_storage
->loadMultiple($matching_config);
$config_entities = $this
->sortConfigEntities($config_entities);
return array_pop($config_entities);
}
/**
* Gets the active entity from using the details from an entity.
*
* @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
* The entity to get the active entity for.
*
* @return \Drupal\Core\Entity\EntityInterface
* The active entity for the provided entity.
*/
public function getFromEntity(ConfigEntityInterface $entity) {
$feature = $entity
->getThirdPartySetting('config_selector', 'feature');
if (!$feature) {
// This is not a config selected entity. Therefore, do not convert the ID.
return $entity;
}
$active_entity = $this
->get($entity
->getEntityTypeId(), $feature);
// If there is no active ID return the entity.
return $active_entity ?: $entity;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ActiveEntity:: |
protected | property | The entity type manager. | |
ActiveEntity:: |
public | function | Gets the active entity from the entity type and config_selector feature. | |
ActiveEntity:: |
public | function | Gets the active entity from using the details from an entity. | |
ActiveEntity:: |
public | function | ActiveEntity constructor. | |
ConfigSelectorSortTrait:: |
protected | function | Sorts an array of configuration entities by priority then config name. |