You are here

public function Paragraph::replace in Search and Replace Scanner 8

Performs the replace operation for the given string/expression.

Parameters

string $field: The field with the matching string (formatted as type:bundle:field).

array $values: An array containing the $form_state values.

array $undo_data: An array containing the data.

Return value

array An array containing the revisoion ids of the affected entities.

Overrides Entity::replace

File

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

Class

Paragraph
Class Paragraph.

Namespace

Drupal\scanner\Plugin\Scanner

Code

public function replace($field, array $values, array $undo_data) {
  list($entityType, $bundle, $fieldname) = explode(':', $field);
  $data = $undo_data;
  $query = \Drupal::entityQuery($entityType);
  $query
    ->condition('type', $bundle);
  if ($values['published']) {
    $query
      ->condition('status', 1);
  }
  $conditionVals = parent::buildCondition($values['search'], $values['mode'], $values['wholeword'], $values['regex'], $values['preceded'], $values['followed']);
  if ($values['language'] !== 'all') {
    $query
      ->condition($fieldname, $conditionVals['condition'], $conditionVals['operator'], $values['language']);
  }
  else {
    $query
      ->condition($fieldname, $conditionVals['condition'], $conditionVals['operator']);
  }
  $entities = $query
    ->execute();
  $paragraphs = \Drupal::entityTypeManager()
    ->getStorage('paragraph')
    ->loadMultiple($entities);
  foreach ($paragraphs as $pid => $paragraph) {
    $paraField = $paragraph
      ->get($fieldname);
    $fieldType = $paraField
      ->getFieldDefinition()
      ->getType();
    if (in_array($fieldType, [
      'text_with_summary',
      'text',
      'text_long',
    ])) {
      $fieldValue = $paraField
        ->getValue()[0];
      $fieldValue['value'] = preg_replace($conditionVals['phpRegex'], $values['replace'], $fieldValue['value']);
      $paragraph->{$fieldname} = $fieldValue;
      if (!isset($data["paragraph:{$pid}"]['new_vid'])) {
        $data["paragraph:{$pid}"]['old_vid'] = $paragraph
          ->getRevisionId();

        // Create a new revision for the paragraph.
        $paragraph
          ->setNewRevision(TRUE);
      }

      // Save the paragraph with the updated field(s).
      $paragraph
        ->save();
      $data["paragraph:{$pid}"]['new_vid'] = $paragraph
        ->getRevisionId();
      $processed = $this
        ->handleParentRelationship($paragraph, $values, $data);
      if ($processed == FALSE) {

        // We couldn't handle the relationship for some reason so we move on
        // to the next paragraph.
        continue;
      }
    }
    elseif ($fieldType == 'string') {
      $fieldValue = preg_replace($conditionVals['phpRegex'], $values['replace'], $paraField
        ->getString());
      $paragraph->{$fieldname} = $fieldValue;
      if (!isset($data["paragraph:{$pid}"]['new_vid'])) {
        $data["paragraph:{$pid}"]['old_vid'] = $paragraph
          ->getRevisionId();
        $paragraph
          ->setNewRevision(TRUE);
      }
      $paragraph
        ->save();
      $data["paragraph:{$pid}"]['new_vid'] = $paragraph
        ->getRevisionId();
      $processed = $this
        ->handleParentRelationship($paragraph, $values, $data);
      if ($processed == FALSE) {

        // We couldn't handle the relationship for some reason so we move on
        // to the next paragraph.
        continue;
      }
    }
  }
  return $data;
}