ParagraphBlocksDeriver.php in Paragraph blocks 8.2
File
src/Plugin/Deriver/ParagraphBlocksDeriver.php
View source
<?php
namespace Drupal\paragraph_blocks\Plugin\Deriver;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\ctools\Plugin\Deriver\EntityDeriverBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParagraphBlocksDeriver extends EntityDeriverBase {
public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, EntityFieldManagerInterface $entity_field_manager, EntityTypeRepositoryInterface $entity_type_repository, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->stringTranslation = $string_translation;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeRepository = $entity_type_repository;
$this->maxCardinality = $config_factory
->get('paragraph_blocks.settings')
->get('max_cardinality') ?: 10;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('entity_type.manager'), $container
->get('string_translation'), $container
->get('entity_field.manager'), $container
->get('entity_type.repository'), $container
->get('config.factory'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->entityFieldManager
->getFieldMap() as $entity_type_id => $field_info) {
if ($entity_type_id == 'paragraph') {
continue;
}
foreach ($this->entityFieldManager
->getFieldStorageDefinitions($entity_type_id) as $field_storage_definition) {
$field_name = $field_storage_definition
->getName();
$field_storage_type = $field_storage_definition
->getType();
if (!isset($field_info[$field_name]) || $field_storage_definition
->getType() != 'entity_reference_revisions' || $field_storage_definition
->getSettings()['target_type'] != 'paragraph') {
continue;
}
$cardinality = $field_storage_definition
->getCardinality();
if ($cardinality == 1) {
continue;
}
if ($cardinality === -1) {
$cardinality = $this->maxCardinality;
}
$bundles = $field_info[$field_name]['bundles'];
foreach ($bundles as $bundle) {
for ($delta = 0; $delta < $cardinality; $delta++) {
$admin_label = $this
->t('Paragraph item @delta', [
'@delta' => $delta,
]);
if (count($bundles) > 1) {
$bundle_field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity_type_id, $bundle);
$bundle_label = $bundle_field_definitions[$field_name]
->getLabel();
$admin_label .= '(' . $bundle_label . ')';
}
$plugin_id = "{$entity_type_id}:{$field_name}:{$delta}:{$bundle}";
$this->derivatives[$plugin_id] = [
'context_definitions' => [
'entity' => EntityContextDefinition::fromEntityTypeId($entity_type_id),
],
'admin_label' => $admin_label,
] + $base_plugin_definition;
}
}
}
}
return $this->derivatives;
}
}