You are here

class ParagraphBlocksDeriver in Paragraph blocks 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Deriver/ParagraphBlocksDeriver.php \Drupal\paragraph_blocks\Plugin\Deriver\ParagraphBlocksDeriver
  2. 3.x src/Plugin/Deriver/ParagraphBlocksDeriver.php \Drupal\paragraph_blocks\Plugin\Deriver\ParagraphBlocksDeriver

Provides entity field block definitions for every field.

Hierarchy

Expanded class hierarchy of ParagraphBlocksDeriver

File

src/Plugin/Deriver/ParagraphBlocksDeriver.php, line 11

Namespace

Drupal\paragraph_blocks\Plugin\Deriver
View source
class ParagraphBlocksDeriver extends EntityDeriverBase {

  // @todo: make this configuration
  const MAX_CARDINALITY = 50;

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $entity_type_labels = $this->entityManager
      ->getEntityTypeLabels();
    foreach ($this->entityManager
      ->getFieldMap() as $entity_type_id => $field_info) {
      if ($entity_type_id == 'paragraph') {
        continue;
      }

      /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage_definition */
      foreach ($this->entityManager
        ->getFieldStorageDefinitions($entity_type_id) as $field_storage_definition) {
        $field_name = $field_storage_definition
          ->getName();
        $field_storage_type = $field_storage_definition
          ->getType();
        if ($field_storage_definition
          ->getType() != 'entity_reference_revisions' || $field_storage_definition
          ->getSettings()['target_type'] != 'paragraph') {
          continue;
        }

        // Get the field's label.
        $first_bundle = reset($field_info[$field_name]['bundles']);
        $bundle_field_definitions = $this->entityManager
          ->getFieldDefinitions($entity_type_id, $first_bundle);
        $admin_label = $bundle_field_definitions[$field_name]
          ->getLabel();

        // Create a plugin of maximum number of cardinality this field allows.
        // Unavailable items are removed in hook_panels_ipe_blocks_alter().
        // Labels are overridden in hook_panels_ipe_blocks_alter().
        $cardinality = $field_storage_definition
          ->getCardinality();
        if ($cardinality == 1) {

          // Skip fields with cardinality one. This can be handled as a field.
          continue;
        }
        if ($cardinality === -1) {
          $cardinality = ParagraphBlocksDeriver::MAX_CARDINALITY;
        }
        for ($delta = 0; $delta < $cardinality; $delta++) {
          $plugin_id = "{$entity_type_id}:{$field_name}:{$delta}";
          $this->derivatives[$plugin_id] = [
            'context' => [
              'entity' => new ContextDefinition('entity:' . $entity_type_id, $entity_type_labels[$entity_type_id], TRUE),
            ],
          ] + $base_plugin_definition;
        }
      }
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
EntityDeriverBase::$entityFieldManager protected property The entity field manager.
EntityDeriverBase::$entityTypeManager protected property The entity type manager.
EntityDeriverBase::$entityTypeRepository protected property The entity type repository.
EntityDeriverBase::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
EntityDeriverBase::__construct public function Constructs new EntityViewDeriver.
ParagraphBlocksDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
ParagraphBlocksDeriver::MAX_CARDINALITY constant
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.