You are here

function acquia_contenthub_subscriber_migrate_filter in Acquia Content Hub 8.2

Converts a Legacy Content Hub Filter to a Cloud Filter.

Parameters

array $data: An array of legacy Content Hub Filter.

Return value

array The Cloud Filter ready to be saved in Content Hub.

1 call to acquia_contenthub_subscriber_migrate_filter()
AcquiaContentHubSubscriberCommands::upgrade in modules/acquia_contenthub_subscriber/src/Commands/AcquiaContentHubSubscriberCommands.php
Subscriber Upgrade Command.

File

modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.filters.migrate.inc, line 20
Defines filter migrations from 1.x to 2.x.

Code

function acquia_contenthub_subscriber_migrate_filter(array $data) {

  // Test all supported languages.
  $supported_languages = array_keys(\Drupal::languageManager()
    ->getLanguages(LanguageInterface::STATE_ALL));

  // Building ES query.
  $query = [
    'query' => [
      'bool' => [
        'filter' => [
          [
            'term' => [
              'data.type' => 'drupal8_content_entity',
            ],
          ],
        ],
      ],
    ],
    'highlight' => [
      'fields' => [
        '*' => new \stdClass(),
      ],
    ],
  ];

  // Adding search term.
  $search_term = [];
  if (!empty($data['search_term'])) {
    foreach ($supported_languages as $language) {
      $search_term[] = [
        'match' => [
          "data.attributes.label.value.{$language}" => $data['search_term'],
        ],
      ];
    }
    $query['query']['bool']['filter'][] = [
      'bool' => [
        'should' => $search_term,
        'minimum_should_match' => 1,
      ],
    ];
  }

  // Adding entity types.
  if (!empty($data['entity_types'])) {
    $query['query']['bool']['filter'][] = [
      'terms' => [
        'data.attributes.entity_type.value.und' => $data['entity_types'],
      ],
    ];
  }

  // Adding bundles.
  if (!empty($data['bundles'])) {
    $bundles = [];
    foreach ($data['bundles'] as $bundle) {
      $bundles[] = [
        'term' => [
          'data.attributes.bundle.value.und' => $bundle,
        ],
      ];
    }
    $query['query']['bool']['filter'][] = [
      'bool' => [
        'should' => $bundles,
      ],
    ];
  }

  // Adding origin.
  if (!empty($data['source'])) {
    $origins = explode(',', $data['source']);
    foreach ($origins as $origin) {
      if (Uuid::isvalid($origin)) {
        $match_origin[] = [
          'match' => [
            'data.origin' => $origin,
          ],
        ];
      }
    }
    if (isset($match_origin)) {
      $query['query']['bool']['filter'][] = [
        'bool' => [
          'should' => $match_origin,
          'minimum_should_match' => 1,
        ],
      ];
    }
  }

  // Tags.
  if (!empty($data['tags'])) {
    $tags = explode(',', $data['tags']);
    foreach ($tags as $tag) {
      if (Uuid::isvalid($tag)) {
        $match_tags[] = [
          'bool' => [
            'should' => [
              [
                'match' => [
                  'data.uuid' => $tag,
                ],
              ],
              [
                'match' => [
                  'data.attributes.tags.value.und' => $tag,
                ],
              ],
            ],
          ],
        ];
      }
    }
    if (!empty($match_tags)) {
      $query['query']['bool']['filter'][] = [
        'bool' => [
          'should' => $match_tags,
          'minimum_should_match' => 1,
        ],
      ];
    }
  }

  // Modified Date.
  $date_modified['time_zone'] = '+01:00';
  if (!empty($data['from_date'])) {
    $date_modified['gte'] = $data['from_date'];
  }
  if (!empty($data['to_date'])) {
    $date_modified['lte'] = $data['to_date'];
  }
  if (!empty($data['from_date']) || !empty($data['to_date'])) {
    $query['query']['bool']['filter'][] = [
      'range' => [
        'data.modified' => $date_modified,
      ],
    ];
  }

  // Should we include sorting in the filter?
  $query['sort'] = [
    'data.modified' => 'desc',
  ];
  return [
    'name' => $data['name'],
    'data' => $query,
    'metadata' => [
      'search_criterion' => $data,
    ],
  ];
}