You are here

public function PhotoswipeFieldFormatter::onDependencyRemoval in PhotoSwipe 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php \Drupal\photoswipe\Plugin\Field\FieldFormatter\PhotoswipeFieldFormatter::onDependencyRemoval()
  2. 3.x src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php \Drupal\photoswipe\Plugin\Field\FieldFormatter\PhotoswipeFieldFormatter::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

src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php, line 204

Class

PhotoswipeFieldFormatter
Plugin implementation of the 'photoswipe_field_formatter' formatter.

Namespace

Drupal\photoswipe\Plugin\Field\FieldFormatter

Code

public function onDependencyRemoval(array $dependencies) {
  $changed = parent::onDependencyRemoval($dependencies);
  $style_ids = [];
  $style_ids['photoswipe_node_style'] = $this
    ->getSetting('photoswipe_node_style');
  if (!empty($this
    ->getSetting('photoswipe_node_style_first'))) {
    $style_ids['photoswipe_node_style_first'] = $this
      ->getSetting('photoswipe_node_style_first');
  }
  $style_ids['photoswipe_image_style'] = $this
    ->getSetting('photoswipe_image_style');

  /** @var \Drupal\image\ImageStyleInterface $style */
  foreach ($style_ids as $name => $style_id) {
    if ($style_id && ($style = ImageStyle::load($style_id))) {
      if (!empty($dependencies[$style
        ->getConfigDependencyKey()][$style
        ->getConfigDependencyName()])) {
        $replacement_id = $this->imageStyleStorage
          ->getReplacementId($style_id);

        // If a valid replacement has been provided in the storage, replace
        // the image style with the replacement and signal that the formatter
        // plugin settings were updated.
        if ($replacement_id && ImageStyle::load($replacement_id)) {
          $this
            ->setSetting($name, $replacement_id);
          $changed = TRUE;
        }
      }
    }
  }
  return $changed;
}