EntityFieldDeriver.php in Chaos Tool Suite (ctools) 8.3
File
modules/ctools_block/src/Plugin/Deriver/EntityFieldDeriver.php
View source
<?php
namespace Drupal\ctools_block\Plugin\Deriver;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\ctools\Plugin\Deriver\EntityDeriverBase;
class EntityFieldDeriver extends EntityDeriverBase {
public function getDerivativeDefinitions($base_plugin_definition) {
$entity_type_labels = $this->entityTypeRepository
->getEntityTypeLabels();
foreach ($this->entityFieldManager
->getFieldMap() as $entity_type_id => $entity_field_map) {
foreach ($this->entityFieldManager
->getFieldStorageDefinitions($entity_type_id) as $field_storage_definition) {
$field_name = $field_storage_definition
->getName();
if (!isset($entity_field_map[$field_name])) {
continue;
}
$field_info = $entity_field_map[$field_name];
$derivative_id = $entity_type_id . ":" . $field_name;
if ($field_storage_definition
->isBaseField()) {
$admin_label = $field_storage_definition
->getLabel();
}
else {
$first_bundle = reset($field_info['bundles']);
$bundle_field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $first_bundle);
if (empty($bundle_field_definitions[$field_name])) {
continue;
}
$admin_label = $bundle_field_definitions[$field_name]
->getLabel();
}
$derivative = $base_plugin_definition;
$derivative['category'] = $this
->t('@entity', [
'@entity' => $entity_type_labels[$entity_type_id],
]);
$derivative['admin_label'] = $admin_label;
$derivative['context_definitions'] = [
'entity' => new EntityContextDefinition('entity:' . $entity_type_id, $entity_type_labels[$entity_type_id], TRUE),
];
$this->derivatives[$derivative_id] = $derivative;
}
}
return $this->derivatives;
}
}