Removing a relationship to an entity reference field can break a View

Created on 30 July 2018, over 6 years ago
Updated 12 June 2024, 5 months ago

I have encountered an issue on multiple occasions since starting to build sites in D8 18 months ago. I can't find a matching issue in the Views queue that covers this, although have seen a couple of issues elsewhere that are possibly related/the same.

However, I've finally figured out how to reproduce it. I'm running Drupal 8.5.5.

The issue causes a View in development to become un-editable. The only workaround that I can find is to return to the main Views page and duplicate the view. This will give you a copy of the last saved version of the view, and you will lose any changes made since saving the view.

Steps to reproduce this:

  1. Create two content types (e.g. Testimonial and Services)
  2. Add an entity reference field to the Testimonial content type that references the Services content type.
  3. Create a view
  4. Add a block to the view
  5. Add fields from the Testimonial content type
  6. Add a Filter for the Testimonial content type, Content: Content type (= Testimonial)
  7. Add a Relationship to Services via the entity reference, Content referenced from field_related_services
  8. Edit the content type filter and specify the relationship you just created.
  9. Click the Relationship, then click Remove.

At this point, the Relationship box will flicker but will not disappear. You can close the box with the X, but if you refresh the screen, it will return the ever-helpful error:

The website encountered an unexpected error. Please try again later.

If you review the server log, you'll see something like this:

[Mon Jul 30 13:01:57.185616 2018] [:error] [pid 364:tid 140679086855936] [client 192.168.1.24.202:53768] Uncaught PHP Exception Exception: "No entity type for field type on view testimonials" at /var/www/html/drupal/web/core/modules/views/src/Plugin/views/HandlerBase.php line 711, referer: http://site.com/admin/structure/views

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
ViewsΒ  β†’

Last updated about 17 hours ago

Created by

πŸ‡³πŸ‡ΏNew Zealand millionleaves

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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.

  • πŸ‡ΊπŸ‡ΈUnited States lpeabody

    I'm running into this, but not because I've removed a relationship, but rather the Bundle filter is trying to access relationship information before I've had a chance to tell it what relationship to use through the UI. This happens because the Bundle filter invokes ::getEntityType in the init function, which fires as soon as you add the filter and before you're prompted with the configuration screen, which is where you would typically specify the relationship.

    Below is my hook_views_data implementation. I'm just exposing a simple table called user_entity_visits with uid, entity_id (currently assumed to be only nodes), entity_type (again, hardcoded to node right now), and timestamp columns.

    The table itself is not a base table for an entity type, it's just a tracking table which denotes the latest point in time a user has visited a node page.

    function harborlife_user_tracking_views_data() {
      $data = [];
      $data['user_entity_visits'] = [];
      $data['user_entity_visits']['table'] = [
        'group' => t('User Tracking'),
        'provider' => 'harborlife_user_tracking',
      ];
      $data['user_entity_visits']['table']['join']['node_field_data'] = [
        'left_field' => 'nid',
        'field' => 'entity_id',
      ];
      $data['user_entity_visits']['table']['join']['users_field_data'] = [
        'left_field' => 'uid',
        'field' => 'uid',
      ];
      $data['user_entity_visits']['entity_id'] = [
        'title' => t('Node ID'),
        'help' => t('Node ID visited by a user.'),
    
        'relationship' => [
          // Views name of the table to join to for the relationship.
          'base' => 'node_field_data',
          // Database field name in the other table to join on.
          'base field' => 'nid',
          // ID of relationship handler plugin to use.
          'id' => 'standard',
          // Default label for relationship in the UI.
          'label' => t('Visited node'),
        ],
      ];
    
      $data['user_entity_visits']['uid'] = [
        'title' => t('User ID'),
        'help' => t('User that visited a node.'),
    
        'relationship' => [
          // Views name of the table to join to for the relationship.
          'base' => 'users_field_data',
          // Database field name in the other table to join on.
          'base field' => 'uid',
          // ID of relationship handler plugin to use.
          'id' => 'standard',
          // Default label for relationship in the UI.
          'label' => t('User who visited node'),
        ],
      ];
    
      $data['user_entity_visits']['timestamp'] = [
        'title' => t('Timestamp'),
        'help' => t('Timestamp of when a user visited a node.'),
        'field' => [
          'id' => 'date',
          'granularity' => 'second',
        ],
        'sort' => [
          // ID of sort handler plugin to use.
          'id' => 'date',
        ],
      ];
    
      return $data;
    }
    
    
  • Status changed to Active over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States lpeabody
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Got bit by this today. Similar to what others have said seems to only be with the Content Type field and when adding a filter. I can add the content type field no issue.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    For me I'm using event_log_track which has a views_data hook.

    Very possible I got that setup wrong
    https://git.drupalcode.org/project/events_log_track/-/blob/4.0.x/modules...
    https://git.drupalcode.org/project/events_log_track/-/blob/4.0.x/event_l...

    But whenever I add the relationship to NodeID and try and use the Content type filter it fails and I have to do some hacky workaround of deleting the view and config importing again to recover.

Production build 0.71.5 2024