public function Node::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/ Node.php, line 95
Class
- Node
- Class Node.
Namespace
Drupal\scanner\Plugin\ScannerCode
public function replace($field, array $values, array $undo_data) {
$data = $undo_data;
if (!is_array($data)) {
$data = [];
}
list($entityType, $bundle, $fieldname) = explode(':', $field);
$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();
foreach ($entities as $id) {
$node = CoreNode::load($id);
$nodeField = $node
->get($fieldname);
$fieldType = $nodeField
->getFieldDefinition()
->getType();
if (in_array($fieldType, [
'text_with_summary',
'text',
'text_long',
])) {
if ($values['language'] === 'all') {
$other_languages = AdminHelper::getAllEnabledLanguages();
foreach ($other_languages as $langcode => $languageName) {
if ($node
->hasTranslation($langcode)) {
$node = $node
->getTranslation($langcode);
$nodeField = $node
->get($fieldname);
}
$fieldValue = $nodeField
->getValue()[0];
// Replace the search term with the replace term.
$fieldValue['value'] = preg_replace($conditionVals['phpRegex'], $values['replace'], $fieldValue['value']);
$node->{$fieldname} = $fieldValue;
}
// This check prevents the creation of multiple revisions if more than
// one field of the same node has been modified.
if (!isset($data["node:{$id}"]['new_vid'])) {
$data["node:{$id}"]['old_vid'] = $node->vid
->getString();
// Crete a new revision so that we can have the option of undoing it
// later on.
$node
->setNewRevision(TRUE);
$node->revision_log = $this
->t('Replaced %search with %replace via Scanner Search and Replace module.', [
'%search' => $values['search'],
'%replace' => $values['replace'],
]);
}
}
else {
$requested_lang = $values['language'];
if ($node
->hasTranslation($requested_lang)) {
$node = $node
->getTranslation($requested_lang);
$nodeField = $node
->get($fieldname);
}
$fieldValue = $nodeField
->getValue()[0];
// Replace the search term with the replace term.
$fieldValue['value'] = preg_replace($conditionVals['phpRegex'], $values['replace'], $fieldValue['value']);
$node->{$fieldname} = $fieldValue;
// This check prevents the creation of multiple revisions if more than
// one field of the same node has been modified.
if (!isset($data["node:{$id}"]['new_vid'])) {
$data["node:{$id}"]['old_vid'] = $node->vid
->getString();
// Crete a new revision so that we can have the option of undoing it
// later on.
$node
->setNewRevision(TRUE);
$node->revision_log = $this
->t('Replaced %search with %replace via Scanner Search and Replace module.', [
'%search' => $values['search'],
'%replace' => $values['replace'],
]);
}
}
// Save the updated node.
$node
->save();
// Fetch the new revision id.
$data["node:{$id}"]['new_vid'] = $node->vid
->getString();
}
elseif ($fieldType == 'string') {
if (!isset($data["node:{$id}"]['new_vid'])) {
if ($values['language'] === 'all') {
$all_languages = AdminHelper::getAllEnabledLanguages();
foreach ($all_languages as $langcode => $languageName) {
if ($node
->hasTranslation($langcode)) {
$node = $node
->getTranslation($langcode);
$nodeField = $node
->get($fieldname);
}
$fieldValue = preg_replace($conditionVals['phpRegex'], $values['replace'], $nodeField
->getString());
$node->{$fieldname} = $fieldValue;
}
$data["node:{$id}"]['old_vid'] = $node->vid
->getString();
$node
->setNewRevision(TRUE);
$node->revision_log = $this
->t('Replaced %search with %replace via Scanner Search and Replace module.', [
'%search' => $values['search'],
'%replace' => $values['replace'],
]);
}
else {
$requested_lang = $values['language'];
if ($node
->hasTranslation($requested_lang)) {
// $nodeField = $nodeField->getTranslation($requested_lang);
$node = $node
->getTranslation($requested_lang);
$nodeField = $node
->get($fieldname);
}
$fieldValue = preg_replace($conditionVals['phpRegex'], $values['replace'], $nodeField
->getString());
$node->{$fieldname} = $fieldValue;
$data["node:{$id}"]['old_vid'] = $node->vid
->getString();
$node
->setNewRevision(TRUE);
$node->revision_log = $this
->t('Replaced %search with %replace via Scanner Search and Replace module.', [
'%search' => $values['search'],
'%replace' => $values['replace'],
]);
}
}
$node
->save();
$data["node:{$id}"]['new_vid'] = $node->vid
->getString();
}
}
return $data;
}