You are here

protected function Paragraph::handleParentRelationship in Search and Replace Scanner 8

Helper function to handle entity reference relationships.

Parameters

mixed $paragraph: The paragraph entity.

array $values: An array containing the input values from the form.

array $data: An array containing the revision id's for the entities being processed.

Return value

bool A boolean which denotes whether or not we were able to process the parent entitiy(s).

1 call to Paragraph::handleParentRelationship()
Paragraph::replace in src/Plugin/Scanner/Paragraph.php
Performs the replace operation for the given string/expression.

File

src/Plugin/Scanner/Paragraph.php, line 205

Class

Paragraph
Class Paragraph.

Namespace

Drupal\scanner\Plugin\Scanner

Code

protected function handleParentRelationship($paragraph, array $values, array &$data) {
  $pid = $paragraph
    ->id();
  $parentEntity = $paragraph
    ->getParentEntity();
  if (empty($parentEntity)) {
    return FALSE;
  }
  $id = $parentEntity
    ->id();
  $parentEntityType = $parentEntity
    ->getEntityTypeId();
  $isProcessed = FALSE;
  if ($parentEntityType == 'node') {
    $parentField = $paragraph
      ->get('parent_field_name')
      ->getString();
    $index = $this
      ->getMultiValueIndex($parentEntity->{$parentField}
      ->getValue(), $pid);

    // Orphaned paragraphs cause issues so we skip them (and their
    // relationships).
    if ($index < 0) {
      \Drupal::logger('scanner')
        ->notice('Unable to find the delta for this paragraph in the parent entity\'s field (id: @id).', [
        '@id' => $pid,
      ]);
      return $isProcessed;
    }
    if (!isset($data["node:{$id}"]['new_vid'])) {
      $data["node:{$id}"]['old_vid'] = $parentEntity
        ->getRevisionId();

      // Create a new revision for the parent entity.
      $parentEntity
        ->setNewRevision(TRUE);
      $parentEntity->revision_log = $this
        ->t('Replaced @search with @replace via Scanner Search and Replace module.', [
        '@search' => $values['search'],
        '@replace' => $values['replace'],
      ]);
    }

    // We need to update the parent entity as well so that it will display
    // the lastest revision.
    $parentEntity->{$parentField}
      ->set($index, [
      'target_id' => $pid,
      'target_revision_id' => $paragraph
        ->getRevisionId(),
    ]);
    $parentEntity
      ->save();
    $data["node:{$id}"]['new_vid'] = $parentEntity
      ->getRevisionId();
    $isProcessed = TRUE;
    return $isProcessed;
  }
  elseif ($parentEntityType == 'paragraph') {
    $grandParentEntity = $parentEntity
      ->getParentEntity();

    // Bail out if grandparent isn't a node, we only handle two levels of
    // nesting.
    if ($grandParentEntity
      ->getEntityTypeId() != 'node') {
      return $isProcessed;
    }
    $parentField = $paragraph
      ->get('parent_field_name')
      ->getString();
    $index = $this
      ->getMultiValueIndex($parentEntity->{$parentField}
      ->getValue(), $pid);

    // Orphaned paragraphs cause issues so we skip them (and their
    // relationships).
    if ($index < 0) {
      \Drupal::logger('scanner')
        ->notice('Unable to find the delta for this paragraph in the parent entity\'s field (id: @id).', [
        '@id' => $pid,
      ]);
      return $isProcessed;
    }

    // Handle parent entity.
    if (!isset($data["paragraph:{$id}"]['new_vid'])) {
      $data["paragraph:{$id}"]['old_vid'] = $parentEntity
        ->getRevisionId();

      // Create a new revision for the paragraph.
      $parentEntity
        ->setNewRevision(TRUE);
    }
    $parentEntity->{$parentField}
      ->set($index, [
      'target_id' => $paragraph
        ->id(),
      'target_revision_id' => $paragraph
        ->getRevisionId(),
    ]);
    $parentEntity
      ->save();
    $data["paragraph:{$id}"]['new_vid'] = $parentEntity
      ->getRevisionId();

    // Handle grandparent entity.
    $grandParentId = $grandParentEntity
      ->id();
    $grandParentField = $parentEntity
      ->get('parent_field_name')
      ->getString();
    $index = $this
      ->getMultiValueIndex($grandParentEntity->{$grandParentField}
      ->getValue(), $id);

    // Orphaned paragraphs can cause issues so we skip them.
    if ($index < 0) {
      \Drupal::logger('scanner')
        ->notice('Unable to find the delta for this paragraph in the parent entity\'s field (id: @id).', [
        '@id' => $id,
      ]);
      return $isProcessed;
    }
    if (!isset($data["node:{$grandParentId}"]['new_vid'])) {
      $data["node:{$grandParentId}"]['old_vid'] = $grandParentEntity
        ->getRevisionId();

      // Create a new revision for the paragraph.
      $grandParentEntity
        ->setNewRevision(TRUE);
      $grandParentEntity->revision_log = $this
        ->t('Replaced @search with @replace via Scanner Search and Replace module.', [
        '@search' => $values['search'],
        '@replace' => $values['replace'],
      ]);
    }
    $grandParentEntity->{$grandParentField}
      ->set($index, [
      'target_id' => $parentEntity
        ->id(),
      'target_revision_id' => $parentEntity
        ->getRevisionId(),
    ]);
    $grandParentEntity
      ->save();
    $data["node:{$grandParentId}"]['new_vid'] = $grandParentEntity
      ->getRevisionId();
    $isProcessed = TRUE;
    return $isProcessed;
  }
  else {

    // Something we didn't expect.
    return $isProcessed;
  }
}