On /media/add/document page can not create Document type media because it causes an error:

Fatal error: Call to a member function isEmpty() on a non-object in /modules/contrib/media_entity_document/src/Plugin/MediaEntity/Type/Document.php on line 46

It happens because Mime type and Size fields of the Document are hidden on /admin/structure/media/manage/document/form-display page

Comments

fox mulder created an issue. See original summary.

fox mulder’s picture

Priority: Normal » Minor

hmmm, it's intresting. First of all:
1. I can not reproduce this on simplytest.me
2. I think so, it's a media_entity_document related issue

There is another symptom / the real source of the fatal error:
on /admin/structure/media/manage/document the Type provider configuration fieldset is empty and because of this in /media_entity_document/src/Plugin/MediaEntity/Type/Document.php -> getField() function the $this->configuration is empty and $media->{$source_field}->entity does not exist ( this causes the fatal error )

I used a workaround:
1. I added buildConfigurationForm() function into /modules/contrib/media_entity_document/src/Plugin/MediaEntity/Type/Document.php ( see patch contents below )
2. I saved the /admin/structure/media/manage/document form
3. Now the fatal error terminates and all things work good ( I can create Document type media entities )
4. I removed the buildConfigurationForm() from /modules/contrib/media_entity_document/src/Plugin/MediaEntity/Type/Document.php

Patch:

diff --git a/src/Plugin/MediaEntity/Type/Document.php b/src/Plugin/MediaEntity/Type/Document.php
index 093fca2..50f4b87 100644
--- a/src/Plugin/MediaEntity/Type/Document.php
+++ b/src/Plugin/MediaEntity/Type/Document.php
@@ -5,6 +5,7 @@ namespace Drupal\media_entity_document\Plugin\MediaEntity\Type;
 use Drupal\media_entity\MediaBundleInterface;
 use Drupal\media_entity\MediaInterface;
 use Drupal\media_entity\MediaTypeBase;
+use Drupal\Core\Form\FormStateInterface;
 
 /**
  * Provides media type plugin for Document.
@@ -56,6 +57,31 @@ class Document extends MediaTypeBase {
   /**
    * {@inheritdoc}
    */
+  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
+    /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
+    $bundle = $form_state->getFormObject()->getEntity();
+    $options = [];
+    $allowed_field_types = ['file', 'image'];
+    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 Document file. 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 thumbnail(MediaInterface $media) {
     $source_field = $this->configuration['source_field'];
     /** @var \Drupal\file\FileInterface $file */

Anybody can reproduce this?