You are here

protected function FilesExtractor::getFileFieldsAndFileEntityItems in Search API attachments 8

Same name and namespace in other branches
  1. 9.0.x src/Plugin/search_api/processor/FilesExtractor.php \Drupal\search_api_attachments\Plugin\search_api\processor\FilesExtractor::getFileFieldsAndFileEntityItems()

Get the file fields of indexed bundles and an entity file general item.

Return value

array An array of file field with field name as key and label as value and an element for generic file entity item.

2 calls to FilesExtractor::getFileFieldsAndFileEntityItems()
FilesExtractor::addFieldValues in src/Plugin/search_api/processor/FilesExtractor.php
Adds the values of properties defined by this processor to the item.
FilesExtractor::getPropertyDefinitions in src/Plugin/search_api/processor/FilesExtractor.php
Retrieves the properties this processor defines for the given datasource.

File

src/Plugin/search_api/processor/FilesExtractor.php, line 473

Class

FilesExtractor
Provides file fields processor.

Namespace

Drupal\search_api_attachments\Plugin\search_api\processor

Code

protected function getFileFieldsAndFileEntityItems() {
  $file_elements = [];

  // Retrieve file fields of indexed bundles.
  foreach ($this
    ->getIndex()
    ->getDatasources() as $datasource) {
    if ($datasource
      ->getPluginId() == 'entity:file') {
      $file_elements[static::SAA_FILE_ENTITY] = $this
        ->t('File entity');
    }
    foreach ($datasource
      ->getPropertyDefinitions() as $property) {
      if ($property instanceof FieldDefinitionInterface) {
        if ($property
          ->getType() == 'file') {
          $file_elements[$property
            ->getName()] = $property
            ->getLabel();
        }
        if ($property
          ->getType() == "entity_reference") {
          if ($property
            ->getSetting('target_type') === 'media') {
            $settings = $property
              ->getItemDefinition()
              ->getSettings();
            if (isset($settings['handler_settings']['target_bundles'])) {

              // For each media bundle allowed, check if the source field is a
              // file field.
              foreach ($settings['handler_settings']['target_bundles'] as $bundle_name) {
                try {
                  if (!empty($this->entityTypeManager
                    ->getStorage('media_type')
                    ->load($bundle_name))) {
                    $bundle_configuration = $this->entityTypeManager
                      ->getStorage('media_type')
                      ->load($bundle_name)
                      ->toArray();
                    if (isset($bundle_configuration['source_configuration']['source_field'])) {
                      $source_field = $bundle_configuration['source_configuration']['source_field'];
                      $field_config = $this->entityTypeManager
                        ->getStorage('field_storage_config')
                        ->load(sprintf('media.%s', $source_field))
                        ->toArray();
                      if (isset($field_config['type']) && $field_config['type'] === 'file') {
                        $file_elements[$property
                          ->getName()] = $property
                          ->getLabel();
                      }
                    }
                  }
                } catch (InvalidPluginDefinitionException $e) {
                  watchdog_exception('search_api_attachments', $e);
                  continue;
                } catch (PluginNotFoundException $e) {
                  watchdog_exception('search_api_attachments', $e);
                  continue;
                }
              }
            }
          }
        }
      }
    }
  }
  return $file_elements;
}