Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Commit

Permalink
Revert "Issue #2625854 by marcoscano, eelkeblok, phenaproxima, mtodor…
Browse files Browse the repository at this point in the history
…, slashrsm, chr.fritsch, Sam152, Boobaa, dawehner, Gábor Hojtsy: Provide default source_field when creating new media entity bundles."

This reverts commit 3a39d03.
  • Loading branch information
slashrsm committed Feb 22, 2017
1 parent 92e7008 commit 41a3ac4
Show file tree
Hide file tree
Showing 14 changed files with 15 additions and 431 deletions.
10 changes: 0 additions & 10 deletions config/schema/media_entity.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,3 @@ field.formatter.settings.media_thumbnail:
image_style:
type: string
label: 'Image style'

media_entity.bundle.field_aware_type:
type: mapping
mapping:
source_field:
type: string
label: 'Source field'

media_entity.bundle.type.generic:
type: media_entity.bundle.field_aware_type
7 changes: 0 additions & 7 deletions src/Annotation/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,4 @@ class MediaType extends Plugin {
*/
public $description = '';

/**
* The field types that can be used as a source field for this type.
*
* @var string[]
*/
public $allowed_field_types = [];

}
75 changes: 2 additions & 73 deletions src/Entity/MediaBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace Drupal\media_entity\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityDescriptionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\media_entity\MediaBundleInterface;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\SourceFieldInterface;

/**
* Defines the Media bundle configuration entity.
Expand Down Expand Up @@ -124,7 +121,7 @@ class MediaBundle extends ConfigEntityBundleBase implements MediaBundleInterface
/**
* Default status of this media bundle.
*
* @var bool
* @var array
*/
public $status = TRUE;

Expand Down Expand Up @@ -244,72 +241,4 @@ public function setNewRevision($new_revision) {
$this->new_revision = $new_revision;
}

/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);

// If the handler uses a source field, we'll need to store its name before
// saving. We'd need to double-save if we did this in postSave().
$handler = $this->getType();
if ($handler instanceof SourceFieldInterface) {
$storage = $handler->getSourceField($this)->getFieldStorageDefinition();
// If the field storage is a new (unsaved) config entity, save it.
if ($storage instanceof FieldStorageConfigInterface && $storage->isNew()) {
$storage->save();
}
// Store the field name. We always want to update this value because the
// field name may have changed, or a new field may have been created,
// depending on the user's actions or the handler's behavior.
$configuration = $handler->getConfiguration();
$configuration['source_field'] = $storage->getName();
$this->setTypeConfiguration($configuration);
}
}

/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);

// If the handler is using a source field, we may need to save it if it's
// new. The field storage is guaranteed to exist already because preSave()
// took care of that.
$handler = $this->getType();
if ($handler instanceof SourceFieldInterface) {
$field = $handler->getSourceField($this);

// If the field is new, save it and add it to this bundle's view and form
// displays.
if ($field->isNew()) {
// Ensure the field is saved correctly before adding it to the displays.
$field->save();

$entity_type = $field->getTargetEntityTypeId();
$bundle = $field->getTargetBundle();

if ($field->isDisplayConfigurable('form')) {
// Use the default widget and settings.
$component = \Drupal::service('plugin.manager.field.widget')
->prepareConfiguration($field->getType(), []);

entity_get_form_display($entity_type, $bundle, 'default')
->setComponent($field->getName(), $component)
->save();
}
if ($field->isDisplayConfigurable('view')) {
// Use the default formatter and settings.
$component = \Drupal::service('plugin.manager.field.formatter')
->prepareConfiguration($field->getType(), []);

entity_get_display($entity_type, $bundle, 'default')
->setComponent($field->getName(), $component)
->save();
}
}
}
}

}
2 changes: 1 addition & 1 deletion src/MediaBundleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function save(array $form, FormStateInterface $form_state) {

// Override the "status" base field default value, for this bundle.
$fields = $this->entityFieldManager->getFieldDefinitions('media', $bundle->id());
$media = $this->entityTypeManager->getStorage('media')->create(['bundle' => $bundle->id()]);
$media = $this->entityTypeManager->getStorage('media')->create(array('bundle' => $bundle->id()));
$value = (bool) $form_state->getValue(['options', 'status']);
if ($media->status->value != $value) {
$fields['status']->getConfig($bundle->id())->setDefaultValue($value)->save();
Expand Down
121 changes: 6 additions & 115 deletions src/MediaTypeBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
namespace Drupal\media_entity;

use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Component\Utility\NestedArray;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;

/**
* Base implementation of media type plugin.
*/
abstract class MediaTypeBase extends PluginBase implements SourceFieldInterface, ContainerFactoryPluginInterface {
abstract class MediaTypeBase extends PluginBase implements MediaTypeInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;

/**
Expand Down Expand Up @@ -105,9 +105,7 @@ public function getConfiguration() {
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'source_field' => NULL,
];
return [];
}

/**
Expand Down Expand Up @@ -140,28 +138,7 @@ public function calculateDependencies() {
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = [];

foreach ($this->entityFieldManager->getFieldStorageDefinitions('media') as $field_name => $field) {
$allowed_type = in_array($field->getType(), $this->pluginDefinition['allowed_field_types'], TRUE);
if ($allowed_type && !$field->isBaseField()) {
$options[$field_name] = $field->getLabel();
}
}

// If there are existing fields to choose from, allow the user to reuse one.
if ($options) {
$form['source_field'] = [
'#type' => 'select',
'#title' => $this->t('Field with source information.'),
'#default_value' => $this->configuration['source_field'],
'#empty_option' => $this->t('- Create -'),
'#empty_value' => NULL,
'#options' => $options,
'#description' => $this->t('The field on media items of this type that will store the source information.'),
];
}
return $form;
return [];
}

/**
Expand All @@ -181,90 +158,4 @@ public function getDefaultName(MediaInterface $media) {
return 'media:' . $media->bundle() . ':' . $media->uuid();
}

/**
* {@inheritdoc}
*/
public function getSourceField(MediaBundleInterface $bundle) {
// If we don't know the name of the source field, we definitely need to
// create it.
if (empty($this->configuration['source_field'])) {
return $this->createSourceField($bundle);
}
// Even if we do know the name of the source field, there is no guarantee
// that it already exists. So check for the field and create it if needed.
$field = $this->configuration['source_field'];
$fields = $this->entityFieldManager->getFieldDefinitions('media', $bundle->id());
return isset($fields[$field]) ? $fields[$field] : $this->createSourceField($bundle);
}

/**
* Returns the source field storage definition.
*
* @return \Drupal\Core\Field\FieldStorageDefinitionInterface
* The field storage definition. Will be unsaved if new.
*/
protected function getSourceFieldStorage() {
// If we don't know the name of the source field, we definitely need to
// create its storage.
if (empty($this->configuration['source_field'])) {
return $this->createSourceFieldStorage();
}
// Even if we do know the name of the source field, we cannot guarantee that
// its storage exists. So check for the storage and create it if needed.
$field = $this->configuration['source_field'];
$fields = $this->entityFieldManager->getFieldStorageDefinitions('media');
return isset($fields[$field]) ? $fields[$field] : $this->createSourceFieldStorage();
}

/**
* Creates the source field storage definition.
*
* @return \Drupal\field\FieldStorageConfigInterface
* The unsaved field storage definition.
*/
abstract protected function createSourceFieldStorage();

/**
* Creates the source field definition for a bundle.
*
* @param \Drupal\media_entity\MediaBundleInterface $bundle
* The bundle.
*
* @return \Drupal\field\FieldConfigInterface
* The unsaved field definition. The field storage definition, if new,
* should also be unsaved.
*/
abstract protected function createSourceField(MediaBundleInterface $bundle);

/**
* Determine the name of the source field.
*
* @return string
* The source field name. If one is already stored in configuration, it is
* returned. Otherwise, a new, unused one is generated.
*/
protected function getSourceFieldName() {
if ($this->configuration['source_field']) {
return $this->configuration['source_field'];
}

$base_id = 'field_media_' . $this->getPluginId();
$tries = 0;
$storage = $this->entityTypeManager->getStorage('field_storage_config');

// Iterate at least once, until no field with the generated ID is found.
do {
$id = $base_id;
// If we've tried before, increment and append the suffix.
if ($tries) {
$id .= '_' . $tries;
}
$field = $storage->load('media.' . $id);
$tries++;
}
while ($field);

return $id;
}

}
2 changes: 1 addition & 1 deletion src/MediaTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Drupal\media_entity;

use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Action/DeleteMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function executeMultiple(array $entities) {
* {@inheritdoc}
*/
public function execute($object = NULL) {
$this->executeMultiple([$object]);
$this->executeMultiple(array($object));
}

/**
Expand Down
34 changes: 1 addition & 33 deletions src/Plugin/MediaEntity/Type/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Drupal\media_entity\Plugin\MediaEntity\Type;

use Drupal\Core\Form\FormStateInterface;
use Drupal\media_entity\MediaBundleInterface;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\MediaTypeBase;

Expand All @@ -13,8 +12,7 @@
* @MediaType(
* id = "generic",
* label = @Translation("Generic media"),
* description = @Translation("Generic media type."),
* allowed_field_types = {"string"}
* description = @Translation("Generic media type.")
* )
*/
class Generic extends MediaTypeBase {
Expand Down Expand Up @@ -44,8 +42,6 @@ public function thumbnail(MediaInterface $media) {
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);

$form['text'] = [
'#type' => 'markup',
'#markup' => $this->t("This type provider doesn't need configuration."),
Expand All @@ -54,32 +50,4 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
return $form;
}

/**
* {@inheritdoc}
*/
protected function createSourceFieldStorage() {
return $this->entityTypeManager
->getStorage('field_storage_config')
->create([
'entity_type' => 'media',
'field_name' => $this->getSourceFieldName(),
// Strings are harmless, inoffensive puppies: a good choice for a
// generic media type.
'type' => 'string',
]);
}

/**
* {@inheritdoc}
*/
protected function createSourceField(MediaBundleInterface $bundle) {
/** @var \Drupal\field\FieldConfigInterface $field */
return $this->entityTypeManager
->getStorage('field_config')
->create([
'field_storage' => $this->getSourceFieldStorage(),
'bundle' => $bundle->id(),
]);
}

}
20 changes: 0 additions & 20 deletions src/SourceFieldInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ media_entity.bundle.type.test_type:
test_config_value:
type: string
label: 'Test config value'
source_field:
type: string
label: 'Source field'
Loading

0 comments on commit 41a3ac4

Please sign in to comment.