You are here

public function Formatter::calculateDependencies in Custom Formatters 8.3

Calculates dependencies and stores them in the dependency property.

Return value

$this

Overrides ConfigEntityBase::calculateDependencies

See also

\Drupal\Core\Config\Entity\ConfigDependencyManager

File

src/Entity/Formatter.php, line 46

Class

Formatter
Defines the formatter entity.

Namespace

Drupal\custom_formatters\Entity

Code

public function calculateDependencies() {

  // Custom Formatter Type provider.

  /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $field_type_definitions = $field_type_manager
    ->getDefinitions();

  /** @var string $field_type */
  foreach ($this->field_types as $field_type) {
    if (isset($field_type_definitions[$field_type])) {
      $this
        ->addDependency('module', $field_type_definitions[$field_type]['provider']);
    }
  }

  // Allow formatter type plugins a chance to add dependencies.
  $dependencies = $this
    ->getFormatterType()
    ->calculateDependencies();
  if (!empty($dependencies) && is_array($dependencies)) {
    foreach ($dependencies as $type => $type_dependencies) {
      if (!empty($type_dependencies) && is_array($type_dependencies)) {
        foreach ($type_dependencies as $name) {
          $this
            ->addDependency($type, $name);
        }
      }
    }
  }

  // Custom Formatter Extras.

  /** @var \Drupal\custom_formatters\FormatterExtrasInterface $extras_manager */
  $extras_manager = \Drupal::service('plugin.manager.custom_formatters.formatter_extras');
  $extras = $extras_manager
    ->getDefinitions();
  if (isset($extras) && is_array($extras)) {
    foreach ($extras as $extra) {
      if (!$extra['optional']) {
        $this
          ->addDependency($extra['provider']);
      }
    }
  }
}