diff --git a/modules/media_riddle_marketplace/config/schema/media_riddle_marketplace.schema.yml b/modules/media_riddle_marketplace/config/schema/media_riddle_marketplace.schema.yml index f2c64e4..5429a19 100644 --- a/modules/media_riddle_marketplace/config/schema/media_riddle_marketplace.schema.yml +++ b/modules/media_riddle_marketplace/config/schema/media_riddle_marketplace.schema.yml @@ -6,10 +6,6 @@ media_riddle_marketplace.settings: type: string label: 'Base folder for thumbnails' -media_entity.bundle.type.riddle_marketplace: - type: mapping +media.source.riddle_marketplace: label: 'Riddle type configuration' - mapping: - source_field: - type: string - label: 'Field with riddle shortcode' + type: media.source.field_aware diff --git a/modules/media_riddle_marketplace/media_riddle_marketplace.info.yml b/modules/media_riddle_marketplace/media_riddle_marketplace.info.yml index 73bd540..5279920 100644 --- a/modules/media_riddle_marketplace/media_riddle_marketplace.info.yml +++ b/modules/media_riddle_marketplace/media_riddle_marketplace.info.yml @@ -1,9 +1,9 @@ name: 'Media Riddle Marketplace' -description: 'Integrates riddle module with media_entity' +description: 'Integrates riddle module with media' package: Riddle type: module core: 8.x dependencies: - - media_entity:media_entity + - drupal:media (>= 8.4) - riddle_marketplace:riddle_marketplace diff --git a/modules/media_riddle_marketplace/media_riddle_marketplace.install b/modules/media_riddle_marketplace/media_riddle_marketplace.install index 59a02f5..b2083d3 100644 --- a/modules/media_riddle_marketplace/media_riddle_marketplace.install +++ b/modules/media_riddle_marketplace/media_riddle_marketplace.install @@ -9,7 +9,20 @@ * Implements hook_install(). */ function media_riddle_marketplace_install() { - $source = drupal_get_path('module', 'media_riddle_marketplace') . '/images/icons'; - $destination = \Drupal::config('media_entity.settings')->get('icon_base'); - media_entity_copy_icons($source, $destination); + $source = drupal_get_path('module', 'media_riddle_marketplace') . '/images'; + $destination = \Drupal::config('media.settings')->get('icon_base_uri'); + file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); + + $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/'); + foreach ($files as $file) { + // When reinstalling we don't want to copy the icons when + // they already exist. The icons could be replaced (by a contrib module or + // manually), so we don't want to replace the existing files. Removing the + // files when we uninstall could also be a problem if the files are + // referenced somewhere else. Since showing an error that it was not + // possible to copy the files is also confusing, we silently do nothing. + if (!file_exists($destination . DIRECTORY_SEPARATOR . $file->filename)) { + file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_ERROR); + } + } } diff --git a/modules/media_riddle_marketplace/src/Controller/RiddleImportController.php b/modules/media_riddle_marketplace/src/Controller/RiddleImportController.php index 7ca7397..1938c9f 100644 --- a/modules/media_riddle_marketplace/src/Controller/RiddleImportController.php +++ b/modules/media_riddle_marketplace/src/Controller/RiddleImportController.php @@ -3,7 +3,7 @@ namespace Drupal\media_riddle_marketplace\Controller; use Drupal\Core\Controller\ControllerBase; -use Drupal\media_entity\Entity\Media; +use Drupal\media\Entity\Media; use Drupal\media_riddle_marketplace\RiddleMediaServiceInterface; use Drupal\riddle_marketplace\Exception\NoApiKeyException; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -68,11 +68,11 @@ public function content() { ]; try { - foreach ($this->riddleMediaService->getNewRiddles() as $bundle => $riddles) { - /** @var \Drupal\media_entity\Entity\MediaBundle $bundle */ - $bundle = $this->entityTypeManager()->getStorage('media_bundle') - ->load($bundle); - $sourceField = $bundle->getTypeConfiguration()['source_field']; + foreach ($this->riddleMediaService->getNewRiddles() as $type => $riddles) { + /** @var \Drupal\media\Entity\MediaType $type */ + $type = $this->entityTypeManager()->getStorage('media_type') + ->load($type); + $sourceField = $type->get('source_configuration')['source_field']; foreach ($riddles as $riddle) { @@ -80,7 +80,7 @@ public function content() { get_class($this) . '::import', [ [ - 'bundle' => $bundle->id(), + 'type' => $type->id(), 'source_field' => $sourceField, 'riddle_id' => $riddle, ], @@ -106,7 +106,7 @@ public function content() { */ public static function import(array $data) { Media::create([ - 'bundle' => $data['bundle'], + 'bundle' => $data['type'], $data['source_field'] => $data['riddle_id'], ])->save(); } diff --git a/modules/media_riddle_marketplace/src/Plugin/MediaEntity/Type/Riddle.php b/modules/media_riddle_marketplace/src/Plugin/media/Source/Riddle.php similarity index 62% rename from modules/media_riddle_marketplace/src/Plugin/MediaEntity/Type/Riddle.php rename to modules/media_riddle_marketplace/src/Plugin/media/Source/Riddle.php index 91b312f..5575aff 100644 --- a/modules/media_riddle_marketplace/src/Plugin/MediaEntity/Type/Riddle.php +++ b/modules/media_riddle_marketplace/src/Plugin/media/Source/Riddle.php @@ -1,26 +1,28 @@ get('media_entity.settings')); - $this->configFactory = $config_factory; + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, RiddleFeedServiceInterface $riddleFeed) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory); $this->riddleFeed = $riddleFeed; } @@ -70,6 +73,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->get('entity_type.manager'), $container->get('entity_field.manager'), + $container->get('plugin.manager.field.field_type'), $container->get('config.factory'), $container->get('riddle_marketplace.feed') ); @@ -78,7 +82,7 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function providedFields() { + public function getMetadataAttributes() { return [ 'id' => $this->t('Riddle id'), 'status' => $this->t('Publishing status'), @@ -91,31 +95,7 @@ public function providedFields() { /** * {@inheritdoc} */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $options = []; - $bundle = $form_state->getFormObject()->getEntity(); - $allowed_field_types = ['string', 'string_long']; - foreach ($this->entityFieldManager->getFieldDefinitions('media', $bundle->id()) as $field_name => $field) { - if (in_array($field->getType(), $allowed_field_types) && !$field->getFieldStorageDefinition()->isBaseField()) { - $options[$field_name] = $field->getLabel(); - } - } - - $form['source_field'] = [ - '#type' => 'select', - '#title' => $this->t('Field with source information'), - '#description' => $this->t('Field on media entity that stores riddle embed code. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'), - '#default_value' => empty($this->configuration['source_field']) ? NULL : $this->configuration['source_field'], - '#options' => $options, - ]; - - return $form; - } - - /** - * {@inheritdoc} - */ - public function getField(MediaInterface $media, $name) { + public function getMetadata(MediaInterface $media, $name) { $code = NULL; if (isset($this->configuration['source_field'])) { @@ -145,25 +125,27 @@ public function getField(MediaInterface $media, $name) { if ($riddle) { switch ($name) { case 'title': + case 'default_name': if (isset($riddle['title'])) { return $riddle['title']; } - return FALSE; + return NULL; case 'status': if (isset($riddle['status'])) { return $riddle['status']; } - return FALSE; + return NULL; case 'thumbnail': if (isset($riddle['image'])) { return $riddle['image']; } - return FALSE; + return NULL; case 'thumbnail_local': - $local_uri = $this->getField($media, 'thumbnail_local_uri'); + case 'thumbnail_uri': + $local_uri = $this->getMetadata($media, 'thumbnail_local_uri'); if ($local_uri) { if (file_exists($local_uri)) { return $local_uri; @@ -173,53 +155,26 @@ public function getField(MediaInterface $media, $name) { $directory = dirname($local_uri); file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); - $image_url = $this->getField($media, 'thumbnail'); + $image_url = $this->getMetadata($media, 'thumbnail'); return file_unmanaged_save_data(file_get_contents($image_url), $local_uri, FILE_EXISTS_REPLACE); } } - return FALSE; + return NULL; case 'thumbnail_local_uri': if (isset($riddle['image'])) { return $this->configFactory->get('media_riddle_marketplace.settings') ->get('local_images') . '/' . $code . '.' . pathinfo(parse_url($riddle['image'], PHP_URL_PATH), PATHINFO_EXTENSION); } - return FALSE; - } - } + return parent::getMetadata($media, 'thumbnail_uri'); - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function getDefaultThumbnail() { - return $this->config->get('icon_base') . '/riddle.png'; - } - - /** - * {@inheritdoc} - */ - public function thumbnail(MediaInterface $media) { - if ($local_image = $this->getField($media, 'thumbnail_local')) { - return $local_image; - } - - return $this->getDefaultThumbnail(); - } - - /** - * {@inheritdoc} - */ - public function getDefaultName(MediaInterface $media) { - - if ($title = $this->getField($media, 'title')) { - return $title; + default: + return parent::getMetadata($media, $name); + } } - return parent::getDefaultName($media); + return NULL; } } diff --git a/modules/media_riddle_marketplace/src/RiddleMediaService.php b/modules/media_riddle_marketplace/src/RiddleMediaService.php index 88e725d..13e5efe 100644 --- a/modules/media_riddle_marketplace/src/RiddleMediaService.php +++ b/modules/media_riddle_marketplace/src/RiddleMediaService.php @@ -57,15 +57,15 @@ public function __construct(RiddleFeedServiceInterface $feedService, EntityTypeM */ public function createMediaEntities() { - foreach ($this->getNewRiddles() as $bundle => $riddles) { - /** @var \Drupal\media_entity\MediaBundleInterface $bundle */ - $bundle = $this->entityTypeManager->getStorage('media_bundle') - ->load($bundle); - $sourceField = $bundle->getTypeConfiguration()['source_field']; + foreach ($this->getNewRiddles() as $type => $riddles) { + /** @var \Drupal\media\MediaTypeInterface $type */ + $type = $this->entityTypeManager->getStorage('media_type') + ->load($type); + $sourceField = $type->get('source_configuration')['source_field']; foreach ($riddles as $riddle) { $this->entityTypeManager->getStorage('media')->create([ - 'bundle' => $bundle->id(), + 'bundle' => $type->id(), $sourceField => $riddle, ])->save(); } @@ -81,24 +81,23 @@ public function getNewRiddles() { $riddle_feed_ids = array_column($feed, 'uid'); - /** @var \Drupal\media_entity\MediaBundleInterface[] $riddleBundles */ - $riddleBundles = $this->entityTypeManager->getStorage('media_bundle') + /** @var \Drupal\media\MediaTypeInterface[] $riddleBundles */ + $riddleBundles = $this->entityTypeManager->getStorage('media_type') ->loadByProperties([ - 'type' => 'riddle_marketplace', + 'source' => 'riddle_marketplace', ]); $newRiddles = []; - foreach ($riddleBundles as $riddleBundle) { - - $sourceField = $riddleBundle->getTypeConfiguration()['source_field']; + foreach ($riddleBundles as $type) { + $sourceField = $type->get('source_configuration')['source_field']; $existing_riddle_id = $this->database->select("media__$sourceField", 'n') ->condition("n.${sourceField}_value", $riddle_feed_ids, 'IN') ->fields('n', ["${sourceField}_value"]) ->execute() ->fetchCol(); - $newRiddles[$riddleBundle->id()] = array_diff($riddle_feed_ids, $existing_riddle_id); + $newRiddles[$type->id()] = array_diff($riddle_feed_ids, $existing_riddle_id); } return $newRiddles;