<?php
-namespace Drupal\media_entity_instagram\Plugin\MediaEntity\Type;
+namespace Drupal\media_entity_instagram\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\MediaInterface;
+use Drupal\media\MediaSourceBase;
+use Drupal\media\MediaSourceFieldConstraintsInterface;
use Drupal\media_entity_instagram\InstagramEmbedFetcher;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides media type plugin for Instagram.
*
- * @MediaType(
+ * @MediaSource(
* id = "instagram",
* label = @Translation("Instagram"),
- * description = @Translation("Provides business logic and metadata for Instagram.")
+ * description = @Translation("Provides business logic and metadata for Instagram."),
+ * allowed_field_types = {"string", "string_long", "link"},
+ * default_thumbnail_filename = "instagram.png"
* )
*/
-class Instagram extends MediaTypeBase {
-
- /**
- * Config factory service.
- *
- * @var \Drupal\Core\Config\ConfigFactoryInterface
- */
- protected $configFactory;
+class Instagram extends MediaSourceBase implements MediaSourceFieldConstraintsInterface {
/**
* The instagram fetcher.
* Entity field manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config factory service.
+ * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
+ * The field type plugin manager service.
* @param \Drupal\media_entity_instagram\InstagramEmbedFetcher $fetcher
* Instagram fetcher service.
* @param \GuzzleHttp\Client $httpClient
* Guzzle client.
*/
- public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, InstagramEmbedFetcher $fetcher, Client $httpClient) {
- 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, ConfigFactoryInterface $config_factory, FieldTypePluginManagerInterface $field_type_manager, InstagramEmbedFetcher $fetcher, Client $httpClient) {
+ parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->fetcher = $fetcher;
$this->httpClient = $httpClient;
}
$container->get('entity_type.manager'),
$container->get('entity_field.manager'),
$container->get('config.factory'),
+ $container->get('plugin.manager.field.field_type'),
$container->get('media_entity_instagram.instagram_embed_fetcher'),
$container->get('http_client')
);
/**
* {@inheritdoc}
*/
- public function providedFields() {
+ public function getMetadataAttributes() {
return [
'shortcode' => $this->t('Instagram shortcode'),
'id' => $this->t('Media ID'),
/**
* {@inheritdoc}
*/
- public function getField(MediaInterface $media, $name) {
+ public function getMetadata(MediaInterface $media, $attribute_name) {
+ if ($attribute_name == 'default_name') {
+ // Try to get some fields that need the API, if not available, just use
+ // the shortcode as default name.
+ $username = $this->getMetadata($media, 'username');
+ $id = $this->getMetadata($media, 'id');
+ if ($username && $id) {
+ return $username . ' - ' . $id;
+ }
+ else {
+ $code = $this->getMetadata($media, 'shortcode');
+ if (!empty($code)) {
+ return $code;
+ }
+ }
+ // Fallback to the parent's default name if everything else failed.
+ return parent::getMetadata($media, 'default_name');
+ }
+ elseif ($attribute_name == 'thumbnail_uri') {
+ return $this->getMetadata($media, 'thumbnail_local');
+ }
+
$matches = $this->matchRegexp($media);
if (!$matches['shortcode']) {
return FALSE;
}
- if ($name == 'shortcode') {
+ if ($attribute_name == 'shortcode') {
return $matches['shortcode'];
}
// If we have auth settings return the other fields.
if ($instagram = $this->fetcher->fetchInstagramEmbed($matches['shortcode'])) {
- switch ($name) {
+ switch ($attribute_name) {
case 'id':
if (isset($instagram['media_id'])) {
return $instagram['media_id'];
return 'http://instagram.com/p/' . $matches['shortcode'] . '/media/?size=m';
case 'thumbnail_local':
- $local_uri = $this->getField($media, 'thumbnail_local_uri');
+ $local_uri = $this->getMetadata($media, 'thumbnail_local_uri');
if ($local_uri) {
if (file_exists($local_uri)) {
$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');
$response = $this->httpClient->get($image_url);
if ($response->getStatusCode() == 200) {
/**
* {@inheritdoc}
*/
- public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
- $options = [];
- $bundle = $form_state->getFormObject()->getEntity();
- $allowed_field_types = ['string', 'string_long', 'link'];
- 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 Instagram embed code or URL. 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 attachConstraints(MediaInterface $media) {
- parent::attachConstraints($media);
-
- if (isset($this->configuration['source_field'])) {
- $source_field_name = $this->configuration['source_field'];
- if ($media->hasField($source_field_name)) {
- foreach ($media->get($source_field_name) as &$embed_code) {
- /** @var \Drupal\Core\TypedData\DataDefinitionInterface $typed_data */
- $typed_data = $embed_code->getDataDefinition();
- $typed_data->addConstraint('InstagramEmbedCode');
- }
- }
- }
+ public function getSourceFieldConstraints() {
+ return ['InstagramEmbedCode' => []];
}
/**
* Runs preg_match on embed code/URL.
*
- * @param \Drupal\media_entity\MediaInterface $media
+ * @param \Drupal\media\MediaInterface $media
* Media object.
*
* @return array|bool
return FALSE;
}
- /**
- * {@inheritdoc}
- */
- public function getDefaultThumbnail() {
- return $this->config->get('icon_base') . '/instagram.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) {
- // Try to get some fields that need the API, if not available, just use the
- // shortcode as default name.
- $username = $this->getField($media, 'username');
- $id = $this->getField($media, 'id');
- if ($username && $id) {
- return $username . ' - ' . $id;
- }
- else {
- $code = $this->getField($media, 'shortcode');
- if (!empty($code)) {
- return $code;
- }
- }
-
- return parent::getDefaultName($media);
- }
-
}