/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /** @var \Drupal\media_entity\MediaBundleInterface $bundle */ $form['#entity'] = $bundle = $this->entity; $form_state->set('bundle', $bundle->id()); if ($this->operation == 'add') { $form['#title'] = $this->t('Add media bundle'); } elseif ($this->operation == 'edit') { $form['#title'] = $this->t('Edit %label media bundle', array('%label' => $bundle->label())); } $form['label'] = array('#title' => t('Label'), '#type' => 'textfield', '#default_value' => $bundle->label(), '#description' => t('The human-readable name of this media bundle.'), '#required' => TRUE, '#size' => 30); // @todo: '#disabled' not always FALSE. $form['id'] = array('#type' => 'machine_name', '#default_value' => $bundle->id(), '#maxlength' => 32, '#disabled' => FALSE, '#machine_name' => array('exists' => array('\\Drupal\\media_entity\\Entity\\MediaBundle', 'exists'), 'source' => array('label')), '#description' => t('A unique machine-readable name for this media bundle.')); $form['description'] = array('#title' => t('Description'), '#type' => 'textarea', '#default_value' => $bundle->getDescription(), '#description' => t('Describe this media bundle. The text will be displayed on the <em>Add new media</em> page.')); $plugins = $this->mediaTypeManager->getDefinitions(); $options = array(); foreach ($plugins as $plugin => $definition) { $options[$plugin] = $definition['label']; } $form['type'] = array('#type' => 'select', '#title' => t('Type provider'), '#default_value' => $bundle->getType()->getPluginId(), '#options' => $options, '#description' => t('Media type provider plugin that is responsible for additional logic related to this media.')); $form['type_configuration'] = array('#type' => 'fieldset', '#title' => t('Type provider configuration'), '#tree' => TRUE); foreach ($plugins as $plugin => $definition) { $plugin_configuration = $bundle->getType()->getPluginId() == $plugin ? $bundle->type_configuration : array(); $form['type_configuration'][$plugin] = array('#type' => 'container', '#states' => array('visible' => array(':input[name="type"]' => array('value' => $plugin)))); /** @var \Drupal\media_entity\MediaTypeBase $instance */ $instance = $this->mediaTypeManager->createInstance($plugin, $plugin_configuration); $form['type_configuration'][$plugin] += $instance->buildConfigurationForm([], $form_state); // Store the instance for validate and submit handlers. $this->configurableInstances[$plugin] = $instance; } return $form; }
/** * Test the fields provided by the integration. * * @dataProvider providedFieldsTestCases */ public function testProvidedFields($input, $field, $expected) { $entity = Media::create(['bundle' => 'video', VideoEmbedField::VIDEO_EMBED_FIELD_DEFAULT_NAME => [['value' => $input]]]); $actual = $this->mediaVideoPlugin->getField($entity, $field); $this->assertEquals($expected, $actual); }