View deleted without notice on deletion of content type

Created on 7 February 2020, over 5 years ago
Updated 2 March 2023, over 2 years ago

Problem/Motivation

When a content type is deleted that has fields used in a view and is the _only_ content type using these fields, the entire view is deleted as well without notice. The fields that are affected by removing the content type are indeed listed in the overview of what is going to be deleted, but not the view(s).

Steps to reproduce

- Create a content type
- Add one field to this that does not exist on any other content type
- Create a new View displaying all content (so no filter on content type)
- Add the field to the Views fields
- Save the View
- Delete the content type
- Check the deletion warning, this does NOT show the View
- Press delete
- Check if the View still exists: nope it doesn't

Proposed resolution

TBA

Remaining tasks

Patch
Review
Commit

User interface changes

TBA

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

10.1

Component
Views 

Last updated about 8 hours ago

Created by

🇩🇪Germany Publishing Future Ammerbuch

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand quietone

    I tested this on Drupal 10.1.x, standard install, and confirmed the problem still exists. I have updated the patch a bit. No fix though.

  • Status changed to Postponed: needs info almost 2 years ago
  • 🇷🇴Romania vasike Ramnicu Valcea

    hmm. tricky thing, imho

    If i delete the field, i got the View listed in the confirmation form, But to be updated, which makes sense.

    If i delete the content type with the field, we do not have those dependencies "recursively", so we can get also the fields dependencies.
    And the results, as issue says, it deletes the View - which i don't think it should happen, if we have another content types.

    imho, It should the same as for field, the Dependent Views should be updated only.
    Imagine there could be plenty of Content types and different Views, that actually wants to use this field, if exists.
    It will delete all of the Views, it would be "nicer" to update them.

    So this looks like a complex problem about removing Dependent Configs (or whatever).
    Maybe unify the code used in the "background" and the one used for UI.
    And i don't know if there is an existing issue, already.

    Anyway, i think, this requires some discussions ... at least

    So, i changed the status, maybe we'll have more questions to lead us to a plan, for start.

  • 🇺🇸United States birchy82

    I can confirm this issue happens on Drupal 9.5.10. I recently deleted unused Vocabularys and suddenly the Admin Content view was deleted. I thought I was crazy because right before that I was deleting some unused content types and another set of views were deleted as well.

  • 🇺🇸United States joshua.boltz

    It seems that this issue is still happening in Drupal 10.1, as I've removed a content type and on drush cex I notice the entire view is deleted, which is not ideal because that view is still used in conjunction with other remaining content types.

  • Status changed to Active about 1 year ago
  • 🇳🇿New Zealand quietone

    I think the status here should be active to allow discussion of a solution.

  • 🇮🇳India shani maurya Mumbai

    I tested this on Drupal 10.1.6, standard install, and confirmed the problem still exists.

  • 🇺🇸United States jennypanighetti

    I'm still having this as well. Other config files successfully just remove the portion of the config that was mentioned, but specifically in Views, the entire view config file is deleted. This is a big bug.

  • 🇮🇳India chandraveer singh

    I followed the steps to reproduce the issue but couldn't replicate it. The view appears in the list of all entities scheduled for deletion, which is the expected behavior.

  • 🇸🇬Singapore anish.a Singapore
  • 🇺🇸United States jennypanighetti

    If you have a view where the "Jobs" type is only one of several content types in a given view, the entire view is still deleted. Is that the expected behavior?

  • 🇸🇪Sweden arne_hortell

    This is a major problem in D11.
    If i develop a view in gui and then operate on content types and delete a content type used by the view, the view should NOT be deleted but maybe disabled. As features doesnt really work with D11 yet, it creates a disaster if many views uses the same content type and all of them must be recreated, manually.

    Thats why its a major issue in D11

  • 🇸🇪Sweden arne_hortell

    I dont know if this is a solution

    use Drupal\views\Entity\View;
    use Drupal\Core\Entity\EntityInterface;
    use Drupal\Core\Entity\EntityStorageException;
    
    function mymodule_entity_predelete(EntityInterface $entity) {
        if ($entity->getEntityTypeId() === 'node_type') {
            $content_type_machine_name = $entity->id();
    
            // Load all views.
            $views = View::loadMultiple();
            foreach ($views as $view) {
                $changed = FALSE;
    
                // Loop through all displays in the view.
                foreach ($view->get('display') as $display_id => $display) {
                    if (!isset($display['display_options']['filters'])) {
                        continue;
                    }
    
                    // Check if there is a filter for the content type.
                    if (isset($display['display_options']['filters']['type']) &&
                        in_array($content_type_machine_name, $display['display_options']['filters']['type']['value'])) {
    
                        // Remove the specific content type filter (set to all content types).
                        $display['display_options']['filters']['type']['value'] = [];
                        $view->set('display', $display);
                        $changed = TRUE;
                    }
                }
    
                // If changes were made, save the view.
                if ($changed) {
                    $view->save();
                    \Drupal::messenger()->addStatus(t('Updated view "@view" to remove dependency on content type "@type".', [
                        '@view' => $view->label(),
                        '@type' => $content_type_machine_name,
                    ]));
                }
            }
        }
    }
  • 🇦🇹Austria maxilein

    How about some log messages?

    "view xy about to be deleted by module xy uninstall."

    "view xy's configuration export BEFORE changes: "

    "view xy's configuration export BEFORE changes: "

    "view xy deleted by module xy uninstall. see previous log messages for configuration changes made."

  • 🇬🇧United Kingdom juc1

    I am getting this in Drupal 10 - it seems very strange behaviour that deleting a content type will cause Views to be deleted - I have a View which uses four content types and deleting one of these content types will cause this View with numerous displays to be deleted - why on earth would I want that? And in another case deleting content type X will cause View Y with about 30 displays to be deleted even though I don't think that content type X is used in View Y and there is no info given in the delete content type X warning to tell me where content type X is used in View Y so that I can remove content type X from the View.

    It would be better not to delete Views when deleting a content type - or at least give us the option - do you want to delete Views A, B C ? Deleting a content type can effect how a View will display but this does not mean that the View should be deleted. Instead the Views UI could give a relevant warning - this View display is missing Field Z etc - so that the user has an option to correct / update / re-use the View. Or if I want to delete the View then I can do that in the Views UI.

  • 🇩🇰Denmark ressa Copenhagen

    I think I saw a View getting deleted, even after something as innocent as uninstalling an image formatter module (PhotoSwipe).

    I agree with @maxilein, and would even take it a step further if possible, having robust stop guards. Ideally, a View should never be deleted automatically. The same as when you try to uninstall the Forum module should happen: You are required to delete all forum posts, containers and forums -- only then are you allowed to uninstall the Forum module.

    Is this possible?

    If you request to delete a content type, the user should be prompted: "The content type XYZ is used by View ZYX, please delete that View first." or "The module Photoswipe is used as image formatter in View ZYX, please delete that View first."

Production build 0.71.5 2024