0

I have a paragraph component that has a single field (reference) that calls on other paragraph components. I'd like to remove one of the paragraph reference field options so that users do not accidentally select it, however, when I do this I start receiving errors across my multisite environment that a reference no longer exists. Is there a quick way to check if a paragraph (or any entity) is being used and in which places (preferably one that shows paths) so that I can remove the reference before deleting this paragraph reference option from my paragraph component?

Any and all assistance would be greatly appreciated, thanks!

1

You want to build Views showing "backwards" pointing Entity Reference connections. It can be confusing to call them Child-Parent or Parent-Child, so be careful which stance some resource take.

In the end, you want to list all nodes that reference the "current" node, they point to it through a Paragraph field, which is an Entity Reference field.

You can build that with the help of the "backwards" Relationship that is usually called something like Content referenced from field_myERfield. This Relationship pulls into your views information about other nodes that reference the nodes originally listed in your Views.

If you start with a usual list of nodes, with the field Title, you will see a list of all nodes on your site showing the nodes' titles.

With the use of the mentioned Relationship you can add another Title Field, and set it to use the Relationship (the original Title isn't using any Relationships). This time the Title field will show the Title of the nodes that reference the node showing in the first Title field! So you'll know whether the node you want to delete is referenced elsewhere, or it's safe to delete it.

You can add a Contextual filter for "current" node, to show only results related to a particular node. This way you can delete the first Title field and only keep the one showing the backwards reference. So if you enter a node ID into the Views Preview, you will get a list of all the nodes that reference the node with the ID you entered! This way the interface may be more intuitive to use but it will work without it.

Here are some Q/A you might want to read for more details and clarity:

How to use view's relationships to pull user entity reference data?

Referencing nodes reciprocally

How do I show a list of nodes referenced by a field?

Related content in a view using entity reference

Display related media entities

1

Is there a quick way to check if a paragraph (or any entity) is being used and in which places (preferably one that shows paths) so that I can remove the reference before deleting this paragraph reference option from my paragraph component?

A paragraph can only be used in one place. So I'm first answering the part "or any entity", which also reflects the question title, and address the paragraphs specifics later.

I would do this in two steps. First find all entity reference fields which are configured to target this entity type, then run an entity query for each field.

As example how to find all entities where term #1 is used:

use Drupal\field\Entity\FieldStorageConfig;

$target_id = '1';
$target_type = 'taxonomy_term';

$map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('entity_reference');
$results = [];
foreach ($map as $entity_type => $fields) {
  foreach ($fields as $field_name => $field) {
    // we are only interested in configured fields, not base fields
    $config = FieldStorageConfig::loadByName($entity_type, $field_name);
    if ($config && $config->getSetting('target_type') == $target_type) {
      $ids = \Drupal::entityQuery($entity_type)
        ->condition($field_name, $target_id)
        ->execute();
      $results[$entity_type][$field_name] = $ids;
    }
  }
}

If the field is configured in a paragraph type, the IDs you find are of paragraph entities. Load the paragraph and get the parent entity with $paragraph->getParentEntity(). For nested paragraphs do this multiple times, until you find a non paragraph entity, for example a node.

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.