You are here

public function ViewDisplays::clearDetached in EVA: Entity Views Attachment 8.2

Remove a removed extra field from entity displays.

Run through all entity displays, clear out views that shouldn't be there. This should be called at Views save and module install/remove.

Parameters

string|null $remove_one: Force removal of a particular 'viewname_displayid' EVA.

bool $remove_all: Remove all EVAs.

1 call to ViewDisplays::clearDetached()
ViewDisplays::reset in src/ViewDisplays.php
Reset display configurations and cache when enabling/disabling EVA.

File

src/ViewDisplays.php, line 152

Class

ViewDisplays
EVA utiltity service.

Namespace

Drupal\eva

Code

public function clearDetached($remove_one = NULL, $remove_all = FALSE) {
  $views = $this
    ->get();
  foreach ($views as $entity => $eva_info) {
    $config_names = $this->configFactory
      ->listAll('core.entity_view_display.' . $entity);
    foreach ($config_names as $id) {
      $config = $this->configFactory
        ->getEditable($id);
      $config_data = $config
        ->get();
      foreach ($eva_info as $eva) {
        $eva_field_name = $eva['name'] . '_' . $eva['display'];

        // Eva should be considered for removal if one of these is true:
        // - all evas should be removed (i.e., when module is uninstalled),
        // - the current eva has at least on bundle specified
        // (if no bundles are specified, an eva is attached to all bundles),
        // - the current eva is specifically targeted for removal
        // (i.e., before deleting the display).
        if ($remove_all || !empty($eva['bundles']) || $eva_field_name == $remove_one) {

          // Does the eva exist in this display config?
          if (array_key_exists($eva_field_name, $config_data['content'])) {

            // Remove the eva if one of these is true:
            // - all evas should be removed,
            // - the eva does not list the entity's bundle (any more),
            // - the eva is specifically targeted for removal.
            if ($remove_all || !in_array($config_data['bundle'], $eva['bundles']) || $eva_field_name == $remove_one) {
              unset($config_data['content'][$eva_field_name]);

              // Exposed filter, too, if it's there.
              unset($config_data['content'][$eva_field_name . '_form']);
              $config
                ->setData($config_data);
              $config
                ->save();
            }
          }
        }
      }
    }
  }
}