diff --git a/src/Entity/Paragraph.php b/src/Entity/Paragraph.php index 8a0c689..8ddf054 100644 --- a/src/Entity/Paragraph.php +++ b/src/Entity/Paragraph.php @@ -134,16 +134,28 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface { * {@inheritdoc} */ public function label() { - $label = ''; + $label = '(Orphan paragraph)'; if ($parent = $this->getParentEntity()) { $parent_field = $this->get('parent_field_name')->value; + /** @var \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $values */ $values = $parent->{$parent_field}; - foreach ($values as $key => $value) { - if ($value->entity->id() == $this->id()) { - $label = $parent->label() . ' > ' . $value->getFieldDefinition()->getLabel(); - } else { - // A previous or draft revision or a deleted stale Paragraph. - $label = $parent->label() . ' > ' . $value->getFieldDefinition()->getLabel() . ' (previous revision)'; + if (empty($values)) { + // If there is a parent entity but the paragraph field is empty or none + // of its values match this entity, we assume it is a new revision that + // no longer references this paragraph. + $previous_revision = TRUE; + } + else { + $previous_revision = TRUE; + foreach ($values as $value) { + if ($value->entity->id() == $this->id()) { + $previous_revision = FALSE; + $label = $parent->label() . ' > ' . $value->getFieldDefinition()->getLabel(); + break; + } + } + if ($previous_revision) { + $label = $parent->label() . ' > ' . $values->getFieldDefinition()->getLabel() . ' (previous revision)'; } } } diff --git a/tests/src/Functional/ParagraphsLabelTest.php b/tests/src/Functional/ParagraphsLabelTest.php new file mode 100644 index 0000000..7e981f3 --- /dev/null +++ b/tests/src/Functional/ParagraphsLabelTest.php @@ -0,0 +1,77 @@ +addParagraphedContentType('paragraphed_test'); + + $admin = $this->drupalCreateUser([ + 'create paragraphed_test content', + 'edit any paragraphed_test content', + 'edit behavior plugin settings', + 'administer paragraphs types', + ]); + $this->drupalLogin($admin); + + // Add a text Paragraph type. + $paragraph_type = 'text_paragraph'; + $this->addParagraphsType($paragraph_type); + $this->addFieldtoParagraphType($paragraph_type, 'field_text', 'string'); + + // Create a host node. + $this->drupalGet('node/add/paragraphed_test'); + $edit = [ + 'title[0][value]' => 'Test Node', + 'field_paragraphs[0][subform][field_text][0][value]' => 'Example text', + ]; + $this->drupalPostForm(NULL, $edit, 'Save'); + + // By this point the label should include the parent entity's label and the + // field label. + $paragraph = $this->getLastEntityOfType('paragraph', TRUE); + $this->assertEquals('Test Node > field_paragraphs', $paragraph->label()); + + // Create a new revision without the paragraph and verify the label on the + // paragraph entity reflects that. + /** @var \Drupal\node\NodeInterface $node */ + $node = $this->getLastEntityOfType('node', TRUE); + $node->set('field_paragraphs', []); + $node->setNewRevision(TRUE); + $node->save(); + $paragraph = $this->getLastEntityOfType('paragraph', TRUE); + $this->assertEquals('Test Node > field_paragraphs (previous revision)', $paragraph->label()); + + // Delete the node and check if the label reflects that. + $node->delete(); + $paragraph = $this->getLastEntityOfType('paragraph', TRUE); + $this->assertEquals('(Orphan paragraph)', $paragraph->label()); + } + +}