You are here

public function ParagraphBlocksDeriver::getDerivativeDefinitions 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::getDerivativeDefinitions()
  2. 3.x src/Plugin/Deriver/ParagraphBlocksDeriver.php \Drupal\paragraph_blocks\Plugin\Deriver\ParagraphBlocksDeriver::getDerivativeDefinitions()

Gets the definition of all derivatives of a base plugin.

Parameters

array $base_plugin_definition: The definition array of the base plugin.

Return value

array An array of full derivative definitions keyed on derivative id.

Overrides DeriverBase::getDerivativeDefinitions

See also

getDerivativeDefinition()

File

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

Class

ParagraphBlocksDeriver
Provides entity field block definitions for every field.

Namespace

Drupal\paragraph_blocks\Plugin\Deriver

Code

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;
}