Skip to content

Extending

The modules define no hooks, events, plugins or services. Their classes are plain PHP with static methods, so other code can reuse the field map and the batch operations.

Field map

Drupal\snr\SNRController::entityFields() returns the text columns the forms offer, as nested value objects:

use Drupal\snr\SNRController;

foreach (SNRController::entityFields() as $entity_type_id => $entity_map) {
  foreach ($entity_map->bundles as $bundle => $bundle_map) {
    foreach ($bundle_map->fields as $field_name => $field_map) {
      // $field_map->table          e.g. node__body
      // $field_map->revision_table e.g. node_revision__body, or NULL
      // $field_map->columns        e.g. ['value' => 'body_value', 'summary' => 'body_summary']
      // $field_map->id_column      e.g. entity_id
      // $field_map->type           e.g. text_with_summary
    }
  }
}

Drupal\snr_safe\SNRSafeController::entityFields() returns the same structure limited to revisionable entity types.

Allow-list

SNRController::allowedTargets() lists every table and column the direct replace may change, keyed table:column. SNRController::allowedTarget() returns the allow-listed target for a requested table and column, or NULL. The batch operations call it, so a target built by other code is checked against the entity schema before any query runs.

SNRController::targetsForSelection() turns form keys of the form entity_type:bundle:field:property into targets for the current and revision tables.

Operations

Drupal\snr\SNROperation holds the search text, the replace text and the options. Pass the options as the third constructor argument:

use Drupal\snr\SNROperation;

$operation = new SNROperation('https?://old\.example\.com/(\w+)', '/$1', [
  'case_sensitive' => TRUE,
  'whole_word' => FALSE,
  'regex' => TRUE,
]);
$operation->error();        // NULL, or why the search cannot be used.
$operation->matches($text); // TRUE if $text contains a match.
$operation->apply($text);   // The replaced text, or NULL if PCRE failed.

Running a replacement from code

Build a batch from the same operations the forms use. In a Drush script:

use Drupal\snr\SNRController;
use Drupal\snr\SNROperation;

$operation = new SNROperation('http://www.example.com', 'https://www.example.com');
$batch = [
  'title' => 'Replace',
  'operations' => [],
  'finished' => [SNRController::class, 'replaceBatchFinished'],
];
foreach (SNRController::targetsForSelection(['node:article:body:value']) as $target) {
  $batch['operations'][] = [[SNRController::class, 'replaceBatchOperation'], [$operation, $target]];
}
batch_set($batch);
drush_backend_batch_process();

For revisions, use Drupal\snr_safe\SNRSafeController with Drupal\snr_safe\SNRSafeTarget($entity_type, $bundle, $field, $property). Its replace operation queues entity chunks, so wrap the operations in SNRSafeController::batchProcessor as SNRSafeForm::buildBatch() does.

Changing the forms

Drupal\snr\Form\SNRForm builds its table from entityFields() and targetOptions(), and its batch from buildBatch(). Subclass it and override those methods, as Drupal\snr_safe\Form\SNRSafeForm does, then point a route at the subclass.