X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fmedia_entity_instagram%2Fsrc%2FPlugin%2Fmedia%2FSource%2FInstagram.php;fp=web%2Fmodules%2Fcontrib%2Fmedia_entity_instagram%2Fsrc%2FPlugin%2FMediaEntity%2FType%2FInstagram.php;h=16e388de42edc6ea474448cd247ad8d44b879119;hp=1a97d906748294b9d5ce4d0bd6fe6639fab9e854;hb=9e65bae52407293a5182f19dc57b5628b09e92f4;hpb=af6d1fb995500ae68849458ee10d66abbdcfb252 diff --git a/web/modules/contrib/media_entity_instagram/src/Plugin/MediaEntity/Type/Instagram.php b/web/modules/contrib/media_entity_instagram/src/Plugin/media/Source/Instagram.php similarity index 63% rename from web/modules/contrib/media_entity_instagram/src/Plugin/MediaEntity/Type/Instagram.php rename to web/modules/contrib/media_entity_instagram/src/Plugin/media/Source/Instagram.php index 1a97d9067..16e388de4 100644 --- a/web/modules/contrib/media_entity_instagram/src/Plugin/MediaEntity/Type/Instagram.php +++ b/web/modules/contrib/media_entity_instagram/src/Plugin/media/Source/Instagram.php @@ -1,13 +1,14 @@ 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; } @@ -82,6 +79,7 @@ class Instagram extends MediaTypeBase { $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') ); @@ -100,7 +98,7 @@ class Instagram extends MediaTypeBase { /** * {@inheritdoc} */ - public function providedFields() { + public function getMetadataAttributes() { return [ 'shortcode' => $this->t('Instagram shortcode'), 'id' => $this->t('Media ID'), @@ -116,20 +114,41 @@ class Instagram extends MediaTypeBase { /** * {@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']; @@ -146,7 +165,7 @@ class Instagram extends MediaTypeBase { 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)) { @@ -157,7 +176,7 @@ class Instagram extends MediaTypeBase { $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) { @@ -194,49 +213,14 @@ class Instagram extends MediaTypeBase { /** * {@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 @@ -261,43 +245,4 @@ class Instagram extends MediaTypeBase { 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); - } - }