- public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
- $options = [];
- $bundle = $form_state->getFormObject()->getEntity();
- $allowed_field_types = ['string', 'string_long', 'link'];
- 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 Instagram embed code or URL. 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 attachConstraints(MediaInterface $media) {
- parent::attachConstraints($media);
-
- if (isset($this->configuration['source_field'])) {
- $source_field_name = $this->configuration['source_field'];
- if ($media->hasField($source_field_name)) {
- foreach ($media->get($source_field_name) as &$embed_code) {
- /** @var \Drupal\Core\TypedData\DataDefinitionInterface $typed_data */
- $typed_data = $embed_code->getDataDefinition();
- $typed_data->addConstraint('InstagramEmbedCode');
- }
- }
- }