0

In Drupal 8, I have a requirement from a client that they be able to find a reference of a given asset (file, node, entity, etc) throughout a Drupal site. The ultimate goal is to know that if they want to delete this asset, it's not going to create 404 errors throughout the places on the site where this is referenced in other content.

I'm thinking about a Views page where the name or ID or some type of identifier would be a contextual filter and then would show a list of all nodes or blocks where that relationship exists.

Any thoughts on how to approach solving an issue like this?

0

Have you looked at /admin/structure/views/view/files? Is that what you need?

Otherwise, you might try your creating your own view of files at /admin/structure/views/add.

No matter the view, you will need to check the file before deletion as there will be no functionality to warn admins.

Then for content, I think it would just be a view of content focused on adding the Content Reference field (or whatever you content reference field_name is), which can show where references are coming from.

langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_content_reference
  module:
    - node
    - user
id: content_references
label: 'Content References'
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
core: 8.x
display:
  default:
    display_plugin: default
    id: default
    display_title: Master
    position: 0
    display_options:
      access:
        type: perm
        options:
          perm: 'access content'
      cache:
        type: tag
        options: {  }
      query:
        type: views_query
        options:
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_comment: ''
          query_tags: {  }
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      pager:
        type: full
        options:
          items_per_page: 50
          offset: 0
          id: 0
          total_pages: null
          expose:
            items_per_page: false
            items_per_page_label: 'Items per page'
            items_per_page_options: '5, 10, 25, 50'
            items_per_page_options_all: false
            items_per_page_options_all_label: '- All -'
            offset: false
            offset_label: Offset
          tags:
            previous: '‹ Previous'
            next: 'Next ›'
            first: '« First'
            last: 'Last »'
          quantity: 9
      style:
        type: table
      row:
        type: fields
      fields:
        field_content_reference:
          id: field_content_reference
          table: node__field_content_reference
          field: field_content_reference
          relationship: none
          group_type: group
          admin_label: ''
          label: 'Referenced Content'
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: target_id
          type: entity_reference_label
          settings:
            link: true
          group_column: target_id
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
        title:
          id: title
          table: node_field_data
          field: title
          relationship: none
          group_type: group
          admin_label: ''
          label: 'Referenced By'
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: false
            ellipsis: false
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: true
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings:
            link_to_entity: true
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          entity_type: node
          entity_field: title
          plugin_id: field
      filters:
        status:
          value: '1'
          table: node_field_data
          field: status
          plugin_id: boolean
          entity_type: node
          entity_field: status
          id: status
          expose:
            operator: ''
          group: 1
        field_content_reference_target_id:
          id: field_content_reference_target_id
          table: node__field_content_reference
          field: field_content_reference_target_id
          relationship: none
          group_type: group
          admin_label: ''
          operator: 'not empty'
          value:
            min: ''
            max: ''
            value: ''
          group: 1
          exposed: false
          expose:
            operator_id: ''
            label: ''
            description: ''
            use_operator: false
            operator: ''
            identifier: ''
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
            placeholder: ''
            min_placeholder: ''
            max_placeholder: ''
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
          plugin_id: numeric
      sorts: {  }
      title: 'Content References'
      header: {  }
      footer: {  }
      empty: {  }
      relationships: {  }
      arguments: {  }
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url.query_args
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:field.storage.node.field_content_reference'
  block_1:
    display_plugin: block
    id: block_1
    display_title: Block
    position: 1
    display_options:
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url.query_args
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:field.storage.node.field_content_reference'

enter image description here enter image description here

I am not sure about an all in one solution...

  • For all in one, you could do some views attachments, multiple tabbed displays, views field module, etc. at least to get everything on one path. – Prestosaurus Mar 5 at 17:37
0

In D8 there are some related modules you might want to check out:

In D7 there are some modules you might check and see if they have D8 plans:

There are open issues you may contribute to about this:

Create Views listing back-references

You'll probably have to create a new View for each of the bundles you're using references on (Content, Taxonomy term, File, Comment etc).

The key is to be aware of your project structure, to know where all of the reference fields are. So check your "Manage fields" pages everywhere for reference fields, and note what they reference. Then create a back-reference map from that info so you know which Views you need to create.

You may end up with less Views depending on your structure, you'll need as many as there are entity types that are being referenced. Then if more than one type references it you can use Relationships to add them to the same view. This way you could have taxonomy terms type View that lists all Content and Files that are tagged with it.

Which Relationships and (Contextual) filter exactly needs to be used depends on the particular structure you have. You can use an Exposed filter instead of a Contextual if it's just a tool to check references, but if you want to display a Block on relevant (Edit) pages that would automatically show what references this (smart) then you need the Contextual.

For example, if you have articles referencing other articles through Entity reference field:

  1. Create View with Block display listing articles
  2. Add the Contextual filter for that Entity reference field:
    When the filter value is NOT available
    Provide default value
    Content ID from URL
    When the filter value IS available or a default is provided
    Specify validation criteria
    Content
    Article
    Hide View if the argument doesn't validate, or some other options that makes sense to you.
  3. Place the Block on article pages, or article edit pages, adjust Node from URL and other Block options.

The block will show if there are any articles that reference the one you're viewing.

Other types of connections will require similar or other types of Views configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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