3

I am trying to programmatically delete an existing view’s display (at the same time as deleting a node) but I am not able to find the good implementation.

HOOK_node_delete(Drupal\Core\Entity\EntityInterface $node) {
//[…]
$view = Views::getView("VIEW_MACHINE_NAME");
$display_name = "block_1";
$views_display = $view->storage->getDisplay($display_name);
//[…]

I can access to the view’s display using the view entity (storage) but as the display has protected access I can’t delete it.

Any help?

1 Answer 1

3

DisplayPluginCollection has a remove() method:

$view->displayHandlers->remove($display_id);

But this doesn't work and I couldn't find any code which is trying to actually remove the display from configuration.

So you would have to do this yourself in the view entity (storage):

$view = \Drupal\views\Views::getView('myview');
$storage = $view->storage;
$displays = $storage->get('display');
unset($displays[$display_id]);
$storage->set('display', $displays);
$storage->save();

The problem is this will not run the remove code of the display. For a block display this doesn't matter, but if you delete a page display you might consider running the remove method mentioned above before deleting the display from storage. This will delete the menu links from the page.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.