Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Issue #2912728 by chr.fritsch: Create a port of riddle_marketplace co…
Browse files Browse the repository at this point in the history
…mpatible with media in core API
  • Loading branch information
chr.fritsch authored and chrfritsch committed Oct 6, 2017
1 parent 97b3072 commit 8b43f97
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
19 changes: 16 additions & 3 deletions modules/media_riddle_marketplace/media_riddle_marketplace.install
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,19 +68,19 @@ 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) {

$batch['operations'][] = [
get_class($this) . '::import',
[
[
'bundle' => $bundle->id(),
'type' => $type->id(),
'source_field' => $sourceField,
'riddle_id' => $riddle,
],
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<?php

namespace Drupal\media_riddle_marketplace\Plugin\MediaEntity\Type;
namespace Drupal\media_riddle_marketplace\Plugin\media\Source;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\MediaTypeBase;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaInterface;
use Drupal\riddle_marketplace\RiddleFeedServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides media type plugin for Riddle.
*
* @MediaType(
* @MediaSource(
* id = "riddle_marketplace",
* label = @Translation("Riddle Marketplace"),
* description = @Translation("Provides business logic and metadata for riddle marketplace.")
* description = @Translation("Provides business logic and metadata for riddle marketplace."),
* allowed_field_types = {"string", "string_long"},
* default_thumbnail_filename = "riddle.png"
* )
*/
class Riddle extends MediaTypeBase {
class Riddle extends MediaSourceBase {

/**
* Config factory service.
Expand Down Expand Up @@ -49,14 +51,15 @@ class Riddle extends MediaTypeBase {
* Entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* Entity field manager service.
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
* The field type plugin manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config factory service.
* The config factory service.
* @param \Drupal\riddle_marketplace\RiddleFeedServiceInterface $riddleFeed
* Riddle feed service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, RiddleFeedServiceInterface $riddleFeed) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $config_factory->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;
}

Expand All @@ -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')
);
Expand All @@ -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'),
Expand All @@ -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'])) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

}
25 changes: 12 additions & 13 deletions modules/media_riddle_marketplace/src/RiddleMediaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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;
Expand Down

0 comments on commit 8b43f97

Please sign in to comment.