public function FilesExtractor::addFieldValues in Search API attachments 8
Same name and namespace in other branches
- 9.0.x src/Plugin/search_api/processor/FilesExtractor.php \Drupal\search_api_attachments\Plugin\search_api\processor\FilesExtractor::addFieldValues()
Adds the values of properties defined by this processor to the item.
Parameters
\Drupal\search_api\Item\ItemInterface $item: The item whose field values should be added.
Overrides ProcessorPluginBase::addFieldValues
File
- src/
Plugin/ search_api/ processor/ FilesExtractor.php, line 182
Class
- FilesExtractor
- Provides file fields processor.
Namespace
Drupal\search_api_attachments\Plugin\search_api\processorCode
public function addFieldValues(ItemInterface $item) {
$files = [];
$config = $this->configFactory
->get(static::CONFIGNAME);
$extractor_plugin_id = $config
->get('extraction_method');
// Get the config option to read text files directly.
$this->configuration['read_text_files_directly'] = $config
->get('read_text_files_directly');
if ($extractor_plugin_id != '') {
$configuration = $config
->get($extractor_plugin_id . '_configuration');
$extractor_plugin = $this->textExtractorPluginManager
->createInstance($extractor_plugin_id, $configuration);
// Get the entity.
$entity = $item
->getOriginalObject()
->getValue();
$is_entity_type_file = $entity
->getEntityTypeId() == 'file';
foreach ($this
->getFileFieldsAndFileEntityItems() as $field_name => $label) {
// If the parent entity is not a file, no need to parse the
// saa static::SAA_FILE_ENTITY item.
if (!$is_entity_type_file && $field_name == static::SAA_FILE_ENTITY) {
break;
}
if ($is_entity_type_file && $field_name == static::SAA_FILE_ENTITY) {
$files[] = $entity;
}
$property_path = static::SAA_PREFIX . $field_name;
// A way to load $field.
foreach ($this->fieldHelper
->filterForPropertyPath($item
->getFields(), NULL, $property_path) as $field) {
$all_fids = [];
if ($entity
->hasField($field_name)) {
// Get type to manage media entity reference case.
$type = $entity
->get($field_name)
->getFieldDefinition()
->getType();
if ($type == 'entity_reference') {
/** @var \Drupal\Core\Field\BaseFieldDefinition $field_def */
$field_def = $entity
->get($field_name)
->getFieldDefinition();
if ($field_def
->getItemDefinition()
->getSetting('target_type') === 'media') {
// This is a media field.
$filefield_values = $entity
->get($field_name)
->filterEmptyItems()
->getValue();
foreach ($filefield_values as $media_value) {
$media = Media::load($media_value['target_id']);
if ($media !== NULL) {
$bundle_configuration = $media
->getSource()
->getConfiguration();
if (isset($bundle_configuration['source_field'])) {
/** @var \Drupal\Core\Field\FieldItemListInterface $field_item */
foreach ($media
->get($bundle_configuration['source_field'])
->filterEmptyItems() as $field_item) {
if ($field_item
->getFieldDefinition()
->getType() === 'file') {
$value = $field_item
->getValue();
$all_fids[] = $value['target_id'];
}
}
}
}
}
}
}
elseif ($type == "file") {
$filefield_values = $entity
->get($field_name)
->filterEmptyItems()
->getValue();
foreach ($filefield_values as $filefield_value) {
$all_fids[] = $filefield_value['target_id'];
}
}
$fids = $this
->limitToAllowedNumber($all_fids);
// Retrieve the files.
$files = $this->entityTypeManager
->getStorage('file')
->loadMultiple($fids);
}
if (!empty($files)) {
$extraction = '';
foreach ($files as $file) {
if ($this
->isFileIndexable($file, $item, $field_name)) {
$extraction .= $this
->extractOrGetFromCache($entity, $file, $extractor_plugin);
}
}
$field
->addValue($extraction);
}
}
}
}
}