Paragraph parent entity property is empty when adding new paragraphs

Created on 7 April 2023, about 1 year ago

Problem/Motivation

Hi folks, love this module. :D

I am using a hook_form_FORM_ID_alter() to modify the paragraph edit form (form ID "layout_paragraphs_component_form") like this:

function mymodule_form_layout_paragraphs_component_form_alter(&$form, FormStateInterface $form_state) {
    /* @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
  $paragraph = $form['#paragraph'];
  $parent_entity = $paragraph->getParentEntity();
  if ($parent_entity->type === 'page') {
    $form['field_image']['#access'] = FALSE;
  }
}

In concept, this should get the parent entity (e.g. the node) that contains the paragraph and only modify the fields for a certain node type (page nodes in this case).

The trouble is that Layout Paragraphs has not set the parent entity on the paragraph object prior to generating this form, so there is no way to get the parent entity. If editing an existing paragraph, the code above works.

Steps to reproduce

  • Put the above code in a custom module, or modify layout_paragraphs_form_layout_paragraphs_component_form_alter() directly in the module to try and get the parent entity.
  • Visit /node/add/page or any node type that has a Layout Paragraphs field.
  • Click an add paragraph button to open the Layout Paragraphs modal.
  • Add a new paragraph.
  • Check that the above code does not fire, because $parent_entity will be NULL. It should be the node object.

You can check the way it is supposed to work by instead editing an existing node with an existing paragraph. In this case the $parent_entity will be found as should happen on new paragraphs.

Proposed resolution

Layout Paragraphs needs to call setParentEntity() on the $paragraph object prior to showing the new component form.

Remaining tasks

Review PR.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States quicksketch

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

Comments & Activities

  • Issue created by @quicksketch
  • @quicksketch opened merge request.
  • πŸ‡ΊπŸ‡ΈUnited States quicksketch

    Here's a patch version in addition to the MR.

    The fix is pretty simple, we just need to set the paragraph parent entity right after creating the new paragraph but before displaying the edit form.

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

    In using this approach, I found that it is not perfect because $paragraph->getParentEntity() will return NULL on new nodes. getParentEntity() actually loads an entity from the database, and in the event that it's a new node, it's not in the database yet.

    However, I found that my patch is still useful because it sets the parent entity type and field name, so I can set up a conditional like this:

    function mymodule_form_layout_paragraphs_component_form_alter(&$form, FormStateInterface $form_state) {
        /* @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
      $paragraph = $form['#paragraph'];
      $parent_field_name = $paragraph->get('parent_field_name')->value;
      if ($parent_field_name === 'field_page_content') {
        $form['field_image']['#access'] = FALSE;
      }
    }
    

    And it's possible that getParentEntity() may be refactored to store the actual entity instead of loading from the database if πŸ› Cannot access parent entity in hook_entity_create Needs review is completed.

Production build 0.69.0 2024