You are here

public static function SourceDatabase::getFormatsUsingTag in Media Migration 8

Returns text formats which are formatting the tag in a Drupal 7 instance.

Parameters

\Drupal\Core\Database\Connection $connection: The database connection of a Drupal 7 instance.

string $tag: The HTML tag to look for.

string[]|string|null $field_names: The fields to check for the given tag.

string|null $source_entity_type_id: The source entity type ID to filter for.

string|null $source_bundle: The source bundle to filter for.

Return value

string[] Array of text formats which are formatting the specified tag.

2 calls to SourceDatabase::getFormatsUsingTag()
MediaWysiwygPluginBase::appendProcessor in src/MediaWysiwygPluginBase.php
Appends the media wysiwyg migrate processor to a field.
media_migration_migrate_d7_filter_format_prepare_row in ./media_migration.module
Implements hook_migrate_MIGRATION_ID_prepare_row() for d7_filter_format.

File

src/Utility/SourceDatabase.php, line 148

Class

SourceDatabase
Utility class for source database specific routines.

Namespace

Drupal\media_migration\Utility

Code

public static function getFormatsUsingTag(Connection $connection, string $tag, $field_names = NULL, ?string $source_entity_type_id = NULL, ?string $source_bundle = NULL) : array {
  $field_names = $field_names ?? self::getTextFields($connection, $source_entity_type_id, $source_bundle);
  if (empty($field_names)) {
    return [];
  }
  static $cached;

  // Cache to avoid repeating these queries unnecessarily (when any of the
  // optional parameters is unspecified).
  $cid = implode(':', array_filter([
    $tag,
    is_string($field_names) ? $field_names : implode(',', $field_names),
    $source_entity_type_id,
    $source_bundle,
  ]));
  if (isset($cached[$cid])) {
    return $cached[$cid];
  }

  // Create a (very big) union query.
  $query = NULL;
  foreach ((array) $field_names as $field_name) {
    $revision_table_exists = $connection
      ->schema()
      ->tableExists("field_revision_{$field_name}");
    $table = $revision_table_exists ? "field_revision_{$field_name}" : "field_data_{$field_name}";
    if (!$revision_table_exists && $connection
      ->schema()
      ->tableExists($table)) {
      continue;
    }
    $union_query = $connection
      ->select($table, $field_name)
      ->condition("{$field_name}.{$field_name}_value", '%<' . $tag . ' %', 'LIKE')
      ->groupBy("{$field_name}.{$field_name}_format");
    $union_query
      ->addField($field_name, "{$field_name}_format", 'format');
    if ($source_entity_type_id) {
      $union_query
        ->condition("{$field_name}.entity_type", $source_entity_type_id);
      if ($source_bundle) {
        $union_query
          ->condition("{$field_name}.bundle", $source_bundle);
      }
    }
    if ($query instanceof SelectInterface) {
      $query
        ->union($union_query);
    }
    else {
      $query = $union_query;
    }
  }
  try {

    // The query might return even 'NULL' format.
    $result = array_filter($query
      ->execute()
      ->fetchCol());
    $cached[$cid] = $result;
  } catch (DatabaseExceptionWrapper $e) {
    $cached[$cid] = [];
  }
  return $cached[$cid];
}