diff --git a/src/Feeds/Target/File.php b/src/Feeds/Target/File.php index 254888d2..d85c4227 100644 --- a/src/Feeds/Target/File.php +++ b/src/Feeds/Target/File.php @@ -102,7 +102,9 @@ class File extends EntityReference { $this->client = $client; $this->token = $token; parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $entity_finder); - $this->fileExtensions = array_filter(explode(' ', $this->settings['file_extensions'])); + if (isset($this->settings['file_extensions'])) { + $this->fileExtensions = array_filter(explode(' ', $this->settings['file_extensions'])); + } $this->fileSystem = $file_system; $this->fileRepository = $file_repository; $this->fileConfig = $file_config; diff --git a/src/Feeds/Target/MediaFile.php b/src/Feeds/Target/MediaFile.php new file mode 100644 index 00000000..81870107 --- /dev/null +++ b/src/Feeds/Target/MediaFile.php @@ -0,0 +1,32 @@ + 'file', + 'name' => $entity->label(), + 'uid' => $this->user->id(), + 'field_media_file' => [ + 'target_id' => $target_id, + ], + ]); + } +} diff --git a/src/Feeds/Target/MediaImage.php b/src/Feeds/Target/MediaImage.php new file mode 100644 index 00000000..ea6344da --- /dev/null +++ b/src/Feeds/Target/MediaImage.php @@ -0,0 +1,34 @@ + 'image', + 'name' => $entity->label(), + 'uid' => $this->user->id(), + 'field_media_image' => [ + 'target_id' => $target_id, + 'alt' => $entity->label(), + ], + ]); + } + +} diff --git a/src/Plugin/Type/Target/FieldTargetBase.php b/src/Plugin/Type/Target/FieldTargetBase.php index 24d13017..f1eb20c8 100644 --- a/src/Plugin/Type/Target/FieldTargetBase.php +++ b/src/Plugin/Type/Target/FieldTargetBase.php @@ -55,8 +55,17 @@ abstract class FieldTargetBase extends TargetBase implements ConfigurableTargetI } if (in_array($field_definition->getType(), $definition['field_types'])) { if ($target = static::prepareTarget($field_definition)) { - $target->setPluginId($definition['id']); - $targets[$id] = $target; + // Set Plugin for Media. + $fieldSettings = $field_definition->getFieldStorageDefinition()->getSettings(); + if (isset($fieldSettings['target_type']) && $fieldSettings['target_type'] == 'media') { + $type = array_key_first($field_definition->getSettings()['handler_settings']['target_bundles']); + $target->setPluginId("media_$type"); + $targets[$id] = $target; + } + else { + $target->setPluginId($definition['id']); + $targets[$id] = $target; + } } } } diff --git a/src/Plugin/Type/Target/MediaTargetBase.php b/src/Plugin/Type/Target/MediaTargetBase.php new file mode 100644 index 00000000..ac2d9b59 --- /dev/null +++ b/src/Plugin/Type/Target/MediaTargetBase.php @@ -0,0 +1,167 @@ +user = $user; + $this->pluginDefinition = $plugin_definition; + $this->client = $client; + $this->token = $token; + $fileSettings = $entity_field_manager->getFieldDefinitions('media', 'image')["field_{$this->pluginDefinition['id']}"]->getSettings(); + $this->fileExtensions = explode(' ', $fileSettings['file_extensions']); + $this->settings['uri_scheme'] = $fileSettings['uri_scheme']; + $this->settings['file_directory'] = $fileSettings['file_directory']; + $this->settings['target_type'] = 'media'; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('http_client'), + $container->get('token'), + $container->get('entity_field.manager'), + $container->get('feeds.entity_finder'), + $container->get('file_system'), + $container->get('current_user'), + $container->get('file.repository'), + $container->get('config.factory')->get('system.file') + ); + } + + /** + * {@inheritDoc} + */ + public function setTarget(FeedInterface $feed, EntityInterface $entity, $field_name, array $raw_values) { + $values = []; + $entity_target = $this->getEntityTarget($feed, $entity); + foreach ($raw_values as $delta => $columns) { + try { + // Remove query parameter from URL to prevent invalid extension error. + $columns['target_id'] = strtok($columns['target_id'], '?'); + $this->prepareValue($delta, $columns); + if (!isset($media) && !empty($columns['target_id'])) { + // If we already have a Media entity with the current fid then we + // don't need a new one. + $query = $this->entityTypeManager->getStorage("media")->getQuery(); + if ($mid = $query->condition("field_{$this->pluginDefinition['id']}", $columns['target_id'])->accessCheck(FALSE)->execute()) { + $mids = array_values($mid); + $mid = array_shift($mids); + // Do not add an existing image to avoid duplicates. + if ($entity_target) { + $item_list = $entity_target->get($field_name); + foreach ($item_list->getValue() as $v) { + if ($v['target_id'] == $mid) { + continue 2; + } + } + } + $media = Media::load($mid); + } + else { + // Create a new media entity. + $media = $this->createMedia($entity, $columns['target_id']); + } + $media->setPublished(TRUE)->save(); + // Add media id as target id. + $columns['target_id'] = $media->id(); + } + $values[] = $columns; + unset($media); + } + catch (ReferenceNotFoundException $e) { + // The referenced entity is not found. We need to enforce Feeds to try + // to import the same item again on the next import. + // Feeds stores a hash of every imported item in order to make the + // import process more efficient by ignoring items it has already seen. + // In this case we need to destroy the hash in order to be able to + // import the reference on a next import. + $entity->get('feeds_item')->hash = NULL; + $feed->getState(StateInterface::PROCESS)->setMessage($e->getFormattedMessage(), 'warning', TRUE); + } + catch (EmptyFeedException $e) { + // Nothing wrong here. + } + catch (TargetValidationException $e) { + // Validation failed. + $this->addMessage($e->getFormattedMessage(), 'error'); + } + } + + if (!empty($values) && $entity_target) { + $item_list = $entity_target->get($field_name); + + // Append these values to the existing values. + $values = array_merge($item_list->getValue(), $values); + $item_list->setValue($values); + } + } + + /** + * Creates a Media entity. + * + * @param $entity + * The target object. + * @param $target_id + * The id of the target to set. + * + * @return EntityInterface|null + * The Media entity. + */ + protected function createMedia(EntityInterface $entity, $target_id) { + return NULL; + } +}