diff --git a/src/MediaTypeDefaultFieldTrait.php b/src/MediaTypeDefaultFieldTrait.php new file mode 100644 index 0000000..83d40be --- /dev/null +++ b/src/MediaTypeDefaultFieldTrait.php @@ -0,0 +1,161 @@ + $this->t('- None -')]; + $allowed_field_types = $this->allowedSourceFieldTypes(); + /** @var \Drupal\media_entity\MediaBundleInterface $bundle */ + $bundle = $form_state->getFormObject()->getEntity(); + 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(); + } + } + + // Select the source field. Only show when the bundle is not new, so there + // will potentially be fields to select. + $form['source_field'] = array( + '#type' => 'select', + '#title' => $this->t('Field with source information'), + '#default_value' => empty($this->configuration['source_field']) ? $this->defaultSourceFieldName() : $this->configuration['source_field'], + '#options' => $options, + '#access' => !$bundle->isNew(), + ); + + // Add a checkbox to allow the field being created automatically on save. + $form['create_source_field'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Create a source field automatically when saving this form.'), + '#description' => $this->t('If checked, a default field will be created and used as a source field. If you uncheck the field, you will need to create a field and revisit this form later to select it.'), + '#default_value' => TRUE, + '#access' => $bundle->isNew(), + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function reactOnBundleCreated($bundle_name, $entity_type_id) { + if (!empty($this->configuration['create_source_field'])) { + $this->createDefaultSourceField($bundle_name); + } + } + + /** + * {@inheritdoc} + */ + public function createDefaultSourceField($bundle_name) { + + // @todo Sanity-check: defaultSourceFieldType is actually allowed. + + // Create / load the field storage. + if (!$storage = FieldStorageConfig::loadByName('media', $this->defaultSourceFieldName())) { + $storage = FieldStorageConfig::create([ + 'field_name' => $this->defaultSourceFieldName(), + 'entity_type' => 'media', + 'type' => $this->defaultSourceFieldType(), + ]); + $storage->save(); + } + + // Create the field instance. + FieldConfig::create([ + 'entity_type' => 'media', + 'field_name' => $this->defaultSourceFieldName(), + 'label' => $this->defaultSourceFieldLabel(), + 'required' => TRUE, + 'bundle' => $bundle_name, + ])->save(); + + // Make the field visible on the form display. + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ + $form_display = EntityFormDisplay::create([ + 'targetEntityType' => 'media', + 'bundle' => $bundle_name, + 'mode' => 'default', + 'status' => TRUE, + ]); + $form_display->setComponent($this->defaultSourceFieldName(), [ + 'type' => $this->defaultSourceFieldWidget(), + ])->save(); + + // Make the field visible on the media entity itself. + /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ + $display = EntityViewDisplay::create([ + 'targetEntityType' => 'media', + 'bundle' => $bundle_name, + 'mode' => 'default', + 'status' => TRUE, + ]); + $display->setComponent($this->defaultSourceFieldName(), [ + 'type' => $this->defaultSourceFieldFormatter(), + ])->save(); + } + +}