You are here

public function Entity::search in Search and Replace Scanner 8

Performs the serach 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.

Return value

array An array containing the titles of the entity and a snippet of the matching text.

Overrides ScannerPluginBase::search

2 methods override Entity::search()
Node::search in src/Plugin/Scanner/Node.php
Performs the serach operation for the given string/expression.
Paragraph::search in src/Plugin/Scanner/Paragraph.php
Performs the serach operation for the given string/expression.

File

src/Plugin/Scanner/Entity.php, line 37

Class

Entity
Class Entity.

Namespace

Drupal\scanner\Plugin\Scanner

Code

public function search($field, array $values) {
  $data = [];
  list($entityType) = explode(':', $field);

  // Attempt to load the matching plugin for the matching entity.
  try {
    $plugin = $this->scannerManager
      ->createInstance("scanner_{$entityType}");
    if (empty($plugin)) {
      throw new PluginException($this
        ->t('Unable to load entity type @entity_type.', [
        '@entity_type' => $entityType,
      ]));
    }
  } catch (PluginException $e) {

    // The instance could not be found so fail gracefully and let the user
    // know.
    \Drupal::logger('scanner')
      ->error($e
      ->getMessage());
    \Drupal::messenger()
      ->addError($this
      ->t('An error occured @e:', [
      '@e' => $e
        ->getMessage(),
    ]));
  }

  // Perform the search on the current field.
  $results = $plugin
    ->search($field, $values);
  if (!empty($results)) {
    $data = $results;
  }
  return $data;
}