You are here

function _media_library_configure_form_display in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/media_library.module \_media_library_configure_form_display()

Ensures that the given media type has a media_library form display.

Parameters

\Drupal\media\MediaTypeInterface $type: The media type to configure.

Return value

bool Whether a form display has been created or not.

Throws

\Drupal\Core\Entity\EntityStorageException

3 calls to _media_library_configure_form_display()
media_library_install in core/modules/media_library/media_library.install
Implements hook_install().
media_library_post_update_display_modes in core/modules/media_library/media_library.post_update.php
Create and configure Media Library form and view displays for media types.
_media_library_media_type_form_submit in core/modules/media_library/media_library.module
Submit callback for media type form.

File

core/modules/media_library/media_library.module, line 396
Contains hook implementations for the media_library module.

Code

function _media_library_configure_form_display(MediaTypeInterface $type) {
  $display = EntityFormDisplay::load('media.' . $type
    ->id() . '.media_library');
  if ($display) {
    return FALSE;
  }
  $values = [
    'targetEntityType' => 'media',
    'bundle' => $type
      ->id(),
    'mode' => 'media_library',
    'status' => TRUE,
  ];
  $display = EntityFormDisplay::create($values);

  // Remove all default components.
  foreach (array_keys($display
    ->getComponents()) as $name) {
    $display
      ->removeComponent($name);
  }

  // Expose the name field when it is not mapped.
  if (!in_array('name', $type
    ->getFieldMap(), TRUE)) {
    $display
      ->setComponent('name', [
      'type' => 'string_textfield',
      'settings' => [
        'size' => 60,
      ],
    ]);
  }

  // If the source field is an image field, expose it so that users can set alt
  // and title text.
  $source_field = $type
    ->getSource()
    ->getSourceFieldDefinition($type);
  if ($source_field
    ->isDisplayConfigurable('form') && is_a($source_field
    ->getItemDefinition()
    ->getClass(), ImageItem::class, TRUE)) {
    $type
      ->getSource()
      ->prepareFormDisplay($type, $display);
  }
  return (bool) $display
    ->save();
}