You are here

public function TestFieldWidget::onDependencyRemoval in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldWidget/TestFieldWidget.php \Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidget::onDependencyRemoval()

Informs the plugin that some configuration it depends on will be deleted.

This method allows plugins to keep their configuration up-to-date when a dependency calculated with ::calculateDependencies() is removed. For example, an entity view display contains a formatter having a setting pointing to an arbitrary config entity. When that config entity is deleted, this method is called by the view display to react to the dependency removal by updating its configuration.

This method must return TRUE if the removal event updated the plugin configuration or FALSE otherwise.

Parameters

array $dependencies: An array of dependencies that will be deleted keyed by dependency type. Dependency types are 'config', 'content', 'module' and 'theme'.

Return value

bool TRUE if the plugin configuration has changed, FALSE if not.

Overrides PluginSettingsBase::onDependencyRemoval

See also

\Drupal\Core\Entity\EntityDisplayBase

File

core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldWidget/TestFieldWidget.php, line 97

Class

TestFieldWidget
Plugin implementation of the 'test_field_widget' widget.

Namespace

Drupal\field_test\Plugin\Field\FieldWidget

Code

public function onDependencyRemoval(array $dependencies) {
  $changed = parent::onDependencyRemoval($dependencies);

  // Only the setting 'role' is resolved here. When the dependency related to
  // this setting is removed, is expected that the widget component will be
  // update accordingly in the display entity. The 'role2' setting is
  // deliberately left out from being updated. When the dependency
  // corresponding to this setting is removed, is expected that the widget
  // component will be disabled in the display entity.
  if (!empty($role_id = $this
    ->getSetting('role'))) {
    if (!empty($dependencies['config']["user.role.{$role_id}"])) {
      $this
        ->setSetting('role', 'anonymous');
      $changed = TRUE;
    }
  }
  return $changed;
}