You are here

public function EntityBlockContent::getDerivativeDefinitions in Entityblock 8

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/Derivative/EntityBlockContent.php, line 20
Contains \Drupal\entityblock\Plugin\Derivative\EntityBlockContent.

Class

EntityBlockContent
Retrieves block plugin definitions for all EntityBlocks.

Namespace

Drupal\entityblock\Plugin\Derivative

Code

public function getDerivativeDefinitions($base_plugin_definition) {

  // Find all entityblock fields.
  $field_ids = \Drupal::entityQuery('field_storage_config')
    ->condition('type', 'entityblock')
    ->execute();
  if ($field_ids) {

    // Load all entityblock fields.
    $fields = \Drupal::entityTypeManager()
      ->getStorage('field_storage_config')
      ->loadMultiple($field_ids);
    foreach ($fields as $field) {

      // Load all field data.
      $entity_query = \Drupal::entityQuery($field
        ->getTargetEntityTypeId());
      $entity_query
        ->condition($field
        ->getName() . '.enabled', 1);
      $entity_ids = $entity_query
        ->execute();

      // There are entities of this type.
      if ($entity_ids) {
        $entities = \Drupal::entityTypeManager()
          ->getStorage($field
          ->getTargetEntityTypeId())
          ->loadMultiple($entity_ids);
        foreach ($entities as $entity) {
          foreach ($entity->{$field
            ->getName()} as $delta => $item) {
            $key = $field
              ->uuid() . '|' . $entity
              ->uuid() . '|' . $delta;
            $this->derivatives[$key] = $base_plugin_definition;
            $this->derivatives[$key]['admin_label'] = $entity
              ->label() . ' (' . $item->title . ')';
            $this->derivatives[$key]['config_dependencies']['content'] = [
              $entity
                ->getConfigDependencyName(),
            ];
            $this->derivatives[$key]['config_dependencies'][$field
              ->getConfigDependencyKey()] = [
              $field
                ->getConfigDependencyName(),
            ];
          }
        }
      }
    }
  }
  return parent::getDerivativeDefinitions($base_plugin_definition);
}