Comments

naveenvalecha created an issue. See original summary.

naveenvalecha’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev

Moving to correct branch.

naveenvalecha’s picture

Status: Active » Needs review
StatusFileSize
new7.79 KB

Initial Port with patch #2831274-363: Bring Media entity module to core as Media module
Could use some reviews :)

//Naveen

naveenvalecha’s picture

Title: [PP-2] Media entity facebook 8.x-2.x » [PP-2] Port to the proposed Media core module API
seanb’s picture

Thanks for working on this! Looking good! Besides the review, I noticed the following would be nice for the module (out of scope for this issue though):

  • Default config providing a media type.
  • Tests.

Basic review (I didn't actually install, but this is what I found at first glance):

  1. +++ b/media_entity_facebook.install
    @@ -10,6 +10,11 @@
     function media_entity_facebook_install() {
    

    Probably good to copy hook_requirements as well.

  2. +++ b/media_entity_facebook.install
    @@ -10,6 +10,11 @@
    diff --git a/src/Plugin/Field/FieldFormatter/FacebookEmbedFormatter.php b/src/Plugin/Field/FieldFormatter/FacebookEmbedFormatter.php
    

    Need to fix this in the formatter:

    /** @var \Drupal\media_entity\MediaInterface $media_entity */
    $media_entity = $items->getEntity();
    
  3. +++ b/src/Plugin/media/Source/Facebook.php
    @@ -1,72 +1,45 @@
    +  public function getMetadataAttributes() {
    

    You still need to rename getFields() to getMetadata to provide values for the metadata attributes.

naveenvalecha’s picture

Title: [PP-2] Port to the proposed Media core module API » [PP-2] Facebook Port to the proposed Media core module API
Assigned: Unassigned » naveenvalecha

Thanks, Sean! Assigning to myself for addressing the #5 review.

Added a follow-up for Add Default config providing a media type and provide Tests #2869286: Add Default config providing a media type and provide Tests

//Naveen

naveenvalecha’s picture

Title: [PP-2] Facebook Port to the proposed Media core module API » [PP-1] Facebook Port to the proposed Media core module API
naveenvalecha’s picture

StatusFileSize
new9.96 KB
new2.77 KB

Here's the patch which addressed the following:

  1. #5.1 - Done
  2. #5.2 - Done
  3. #5.3 - getFields was not used in the module.

//Naveen

naveenvalecha’s picture

StatusFileSize
new9.96 KB
new2.77 KB

Here's the correct patch and inter diff.

seanb’s picture

I'm sorry, getFields() was supposed to be getField(). This method needs to be renamed to getMetadata().

naveenvalecha’s picture

#10,

I'm sorry, getFields() was supposed to be getField(). This method needs to be renamed to getMetadata().

getField was not in the head itself.Probably there was nowhere it displayed the field/metadata information. Filed a followup issue to fix in the head #2869286: Add Default config providing a media type and provide Tests because there could be some test case where the metadata would be required.

//Naveen

naveenvalecha’s picture

StatusFileSize
new10.13 KB
new2.49 KB

Thanks for the reviews. here's the updated patch where I fixed more findings.

//Naveen

naveenvalecha’s picture

StatusFileSize
new10.27 KB
new1.07 KB

If the module does not define the media source config schema it throws the WSOD. However, it seems an edge case and module should define the metadata which was missing in the case of media_entity_facebook
Below is the error with backtrace for log.

[16-Apr-2017 12:41:22 Europe/Berlin] Error: Call to a member function getLabel() on null in /Applications/MAMP/htdocs/d8/core/modules/media/src/MediaSourceBase.php on line 197 #0 /Applications/MAMP/htdocs/d8/core/modules/media/src/MediaTypeForm.php(141): Drupal\media\MediaSourceBase->buildConfigurationForm(Array, Object(Drupal\Core\Form\SubformState))
#1 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/Entity/EntityForm.php(115): Drupal\media\MediaTypeForm->form(Array, Object(Drupal\Core\Form\FormState))
#2 [internal function]: Drupal\Core\Entity\EntityForm->buildForm(Array, Object(Drupal\Core\Form\FormState))
#3 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/Form/FormBuilder.php(514): call_user_func_array(Array, Array)
#4 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/Form/FormBuilder.php(271): Drupal\Core\Form\FormBuilder->retrieveForm('media_type_edit...', Object(Drupal\Core\Form\FormState))
#5 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/Controller/FormController.php(74): Drupal\Core\Form\FormBuilder->buildForm('media_type_edit...', Object(Drupal\Core\Form\FormState))
#6 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#7 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#8 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/Render/Renderer.php(574): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#9 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#10 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#11 [internal function]: Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#12 /Applications/MAMP/htdocs/d8/vendor/symfony/http-kernel/HttpKernel.php(144): call_user_func_array(Object(Closure), Array)
#13 /Applications/MAMP/htdocs/d8/vendor/symfony/http-kernel/HttpKernel.php(64): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#14 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /Applications/MAMP/htdocs/d8/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /Applications/MAMP/htdocs/d8/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /Applications/MAMP/htdocs/d8/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#21 /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/DrupalKernel.php(656): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 /Applications/MAMP/htdocs/d8/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#23 {main}

//Naveen

naveenvalecha’s picture

Status: Needs review » Needs work
StatusFileSize
new11.58 KB

Here's the updated patch with more changes. I'm getting this error while saving the facebook media item. Debugging this further. Moving to N/W due to below issue

[17-Apr-2017 11:26:56 Europe/Berlin] Uncaught PHP Exception Drupal\Component\Plugin\Exception\PluginNotFoundException: "The "0" plugin does not exist." at /Applications/MAMP/htdocs/d8/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php line 52

//Naveen

naveenvalecha’s picture

StatusFileSize
new11.59 KB
marcoscano’s picture

Title: [PP-1] Facebook Port to the proposed Media core module API » Facebook Port to the proposed Media core module API
Category: Feature request » Task

Nice work!

I'm starting to port another plugins as well, and wanted to make sure we are more or less in sync. Here are the doubts I had while reviewing this patch:

  1. +++ b/media_entity_facebook.info.yml
    @@ -4,4 +4,5 @@ type: module
    +#  - drupal:system (>= 8.4.0)
    

    Shouldn't this be uncommented?

  2. +++ b/media_entity_facebook.install
    @@ -10,6 +10,39 @@
    +    $destination = 'public://media-icons/generic';
    

    Shouldn't this be $destination = \Drupal::config('media.settings')->get('icon_base_uri'); ?

  3. +++ b/src/Plugin/Field/FieldFormatter/FacebookEmbedFormatter.php
    @@ -25,14 +25,14 @@ class FacebookEmbedFormatter extends FormatterBase {
    +          '#markup' => FacebookMarkup::create($source->getField($media, 'html')),
    

    Shouldn't $source->getMetadata() be used instead of ->getField() ?

  4. +++ b/src/Plugin/media/Source/Facebook.php
    @@ -1,72 +1,77 @@
    + *   default_thumbnail_filename = "facebook.png",
    

    Minor: unnecessary trailing comma?

  5. +++ b/src/Plugin/media/Source/Facebook.php
    @@ -1,72 +1,77 @@
    +      case 'url':
    +        return $data['url'];
    +
    +      case 'html':
    +        return $data['html'];
    

    Shouldn't we include here the two new metadata fields defined in the base class method as well? Something like:

    case 'default_name':
      return parent::getMetadata($media, 'default_name');
    
    case 'thumbnail_uri':
      return parent::getMetadata($media, 'thumbnail_uri');
    
naveenvalecha’s picture

  1. #16.1 This could be removed now as the main patch is in. Done.
  2. #16.2 I would love to do that but the configuration would not be available during the installation phase of the module. We'll have to rely on the exact value. :( See media.install for more information.
  3. #16.3 Updated. Done
  4. #16.4 Removed. Done
  5. #16.5 That's a good suggestion. Please file a new issue to discuss this as this is out of the scope of this issue. What module author think about exposing those two attributes for the site builder.

P.S.: I have tested the code and it is breaking while saving the new entity. I'll get back to this issue tomorrow if you want me to beat it go ahead.

Steps for testing:
1) Go to the media/add/facebook
2) Save any facebook media
3) BUMP WSOD SQL exception error due to the thumbnail_uri

[21-May-2017 19:14:42 Europe/Berlin] Uncaught PHP Exception Drupal\Core\Entity\EntityStorageException: "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'uri' cannot be null: INSERT INTO {file_managed} (uuid, langcode, uid, filename, uri, filemime, filesize, status, created, changed) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array
(
    [:db_insert_placeholder_0] => da14e962-5f6f-4f0d-8627-97aff94be4fc
    [:db_insert_placeholder_1] => en
    [:db_insert_placeholder_2] => 1
    [:db_insert_placeholder_3] => 
    [:db_insert_placeholder_4] => 
    [:db_insert_placeholder_5] => 
    [:db_insert_placeholder_6] => 
    [:db_insert_placeholder_7] => 1
    [:db_insert_placeholder_8] => 1495386879
    [:db_insert_placeholder_9] => 1495386879
)
" at /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php line 805

Reason:
In the presave of the Media entity, the media item is updating the thumnail using updateThumbnail but it is failed to get the thumnail_uri from the getThumbnailUri method. Below is the getThumbnailUri method of Media class.

  protected function getThumbnailUri($from_queue) {
    $thumbnails_queued = $this->bundle->entity->thumbnailDownloadsAreQueued();
    if ($thumbnails_queued && $this->isNew()) {
      $default_thumbnail_filename = $this->getSource()->getPluginDefinition()['default_thumbnail_filename'];
      $thumbnail_uri = \Drupal::service('config.factory')->get('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename;
    }
    elseif ($thumbnails_queued && !$from_queue) {
      $thumbnail_uri = $this->get('thumbnail')->entity->getFileUri();
    }
    else {
      $thumbnail_uri = $this->getSource()->getMetadata($this, $this->getSource()->getPluginDefinition()['thumbnail_uri_metadata_attribute']);
    }

    return $thumbnail_uri;
  }

Condition:
1) Entity is new
2) Queue thumbnail downloads is not enabled
3) It is going into the else condition, where it is trying to get the thumnail_uri from the thumbnail_uri Annotation metadata which is not set(Actually is not required). The default_thumbnail_filename attribute is set in the Facebook Media source annotation.

Consensus: It should come in the first condition.

Next Steps:
File a new issue.
Write a Test to prove that this is failing.
Post a patch

Setting it to Needs Review to see how many other tests are failing :) Go testbot go!
//Naveen

naveenvalecha’s picture

Status: Needs work » Needs review
StatusFileSize
new11.56 KB
new1.81 KB

Here are the patch files for #16

//Naveen

seanb’s picture

Condition:
1) Entity is new
2) Queue thumbnail downloads is not enabled
3) It is going into the else condition, where it is trying to get the thumnail_uri from the thumbnail_uri Annotation metadata which is not set(Actually is not required). The default_thumbnail_filename attribute is set in the Facebook Media source annotation.

Consensus: It should come in the first condition.

The getMetadata() method doesn't call it's parent if no metadata was found. In MediaSourceBase a default thumbnail is returned for the thumbnail_uri attribute. I think if you add the code below to the switch statement it should already be fixed.

      default:
        return parent::getMetadata($media, $name);
naveenvalecha’s picture

StatusFileSize
new11.64 KB
new451 bytes

Here we go. Addressed above issue in #17.

//Naveen

bkosborne’s picture

StatusFileSize
new14.66 KB

Here's a re-roll since I recently did some refactoring.

bkosborne’s picture

StatusFileSize
new2.77 KB
new14.97 KB

And after testing, here's some fixes.

One thing I couldn't figure out how to do is to automatically set the proper field formatter to the one provided by this module when the field config is created. That would be great for the out of box experience. Otherwise now users have to edit the media entity display settings and manually change the formatter.

bkosborne’s picture

Status: Needs review » Fixed

Committed. Thanks for all the work everyone! Feel free to create follow ups for the 2.x branch. I'll update project homepage to indicate 2.x branch is compatible with media in core.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.