You are here

class ParagraphLineageInspector in Paragraphs Edit 8.2

ParagraphLineageInspector class.

Hierarchy

Expanded class hierarchy of ParagraphLineageInspector

1 string reference to 'ParagraphLineageInspector'
paragraphs_edit.services.yml in ./paragraphs_edit.services.yml
paragraphs_edit.services.yml
1 service uses ParagraphLineageInspector
paragraphs_edit.lineage.inspector in ./paragraphs_edit.services.yml
Drupal\paragraphs_edit\ParagraphLineageInspector

File

src/ParagraphLineageInspector.php, line 11

Namespace

Drupal\paragraphs_edit
View source
class ParagraphLineageInspector {

  /**
   * Gets the root parent of this paragraph.
   *
   * @param \Drupal\paragraphs\ParagraphInterface $paragraph
   *   Paragraph data.
   *
   * @return \Drupal\Core\Entity\ContentEntityInterface|null
   *   Root parent.
   */
  public function getRootParent(ParagraphInterface $paragraph) {
    $root_parent = $paragraph
      ->getParentEntity();
    while ($root_parent instanceof ParagraphInterface) {
      $root_parent = $root_parent
        ->getParentEntity();
    }
    return $root_parent;
  }

  /**
   * Gets the field the paragraph is referenced from.
   *
   * @param \Drupal\paragraphs\ParagraphInterface $paragraph
   *   Paragraph data.
   *
   * @return \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList|null
   *   Parent field.
   */
  public function getParentField(ParagraphInterface $paragraph) {
    $parent = $paragraph
      ->getParentEntity();
    if (!$parent) {
      return NULL;
    }
    return $parent
      ->get($paragraph
      ->get('parent_field_name')->value);
  }

  /**
   * Gets the field item the paragraph is referenced from.
   *
   * @param \Drupal\paragraphs\ParagraphInterface $paragraph
   *   Paragraph data.
   * @param \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList|null $parent_field
   *   Parent field.
   *
   * @return \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem|null
   *   Field item.
   */
  public function getParentFieldItem(ParagraphInterface $paragraph, EntityReferenceRevisionsFieldItemList $parent_field = NULL) {
    if (!$parent_field) {
      $parent_field = $this
        ->getParentField($paragraph);
    }
    return $this
      ->findParentFieldItem($paragraph, $parent_field);
  }

  /**
   * Finds the field item the paragraph is referenced from.
   *
   * @param \Drupal\paragraphs\ParagraphInterface $paragraph
   *   Paragraph data.
   * @param \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $field
   *   Field item.
   *
   * @return \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem|null
   *   Referenced field item.
   */
  protected function findParentFieldItem(ParagraphInterface $paragraph, EntityReferenceRevisionsFieldItemList $field) {
    $paragraph_id = $paragraph
      ->id();
    $paragraph_revision_id = $paragraph
      ->getRevisionId();
    foreach ($field as $item) {
      if ($item->target_id == $paragraph_id && $item->target_revision_id == $paragraph_revision_id) {
        return $item;
      }
    }
    return NULL;
  }

  /**
   * Builds a string representation of a paragraph's lineage.
   *
   * @param \Drupal\paragraphs\ParagraphInterface $paragraph
   *   The paragraph whose lineage to return as a string.
   *
   * @return string
   *   A string representation of the paragraph's lineage.
   */
  public function getLineageString(ParagraphInterface $paragraph) {
    $string = '';
    do {
      $parent = $paragraph
        ->getParentEntity();
      if (!$parent) {
        break;
      }
      $parent_field = $this
        ->getParentField($paragraph);
      $parent_field_label = $parent_field
        ->getFieldDefinition()
        ->getLabel();
      $parent_field_item = $this
        ->getParentFieldItem($paragraph, $parent_field);
      $parent_field_delta = $parent_field_item
        ->getName() + 1;
      $string = ' > ' . $parent_field_label . ' #' . $parent_field_delta . $string;
      $paragraph = $parent;
    } while ($parent instanceof ParagraphInterface);
    if ($parent) {
      return $parent
        ->label() . $string;
    }
    return (string) t('Orphan paragraph');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParagraphLineageInspector::findParentFieldItem protected function Finds the field item the paragraph is referenced from.
ParagraphLineageInspector::getLineageString public function Builds a string representation of a paragraph's lineage.
ParagraphLineageInspector::getParentField public function Gets the field the paragraph is referenced from.
ParagraphLineageInspector::getParentFieldItem public function Gets the field item the paragraph is referenced from.
ParagraphLineageInspector::getRootParent public function Gets the root parent of this paragraph.