Commit 9b034832 authored by Julian Pustkuchen's avatar Julian Pustkuchen
Browse files

Issue #3285560 by Anybody, thomas.frobieter: Add a "Create Slideshow"-Link to...

Issue #3285560 by Anybody, thomas.frobieter: Add a "Create Slideshow"-Link to Media Slideshow Reference Fields (Media Library Widget)
parent 1ee14a73
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -83,3 +83,34 @@ function drowl_media_field_widget_form_alter(&$element, FormStateInterface $form
    }
  }
}

/**
 * Implements hook_field_widget_media_library_widget_form_alter().
 *
 * Adds a "Create new Slideshow" button beneath "Select slideshow" media library
 * widget button. This is a *workaround* to provide a link for slideshow creation
 * as "Slideshow" is a custom media source, which doesn't provide a create itself.
 */
function drowl_media_field_widget_media_library_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  if (!empty($element['#target_bundles']) && in_array("slideshow", $element['#target_bundles'])) {
    // Change the regular "Add"-label, so its clear to the user that this means
    // "Reference to existing Slideshows"
    $element["open_button"]["#value"] = t('Select Slideshow');
    // Add further "Create Slideshow"-Button, opens the Slideshow create form in a new tab
    $element['#field_suffix']['empty_selection'] = [
      '#type' => 'link',
      '#title' => t('Create new Slideshow'),
      '#url' => \Drupal\Core\Url::fromRoute('entity.media.add_form', ['media_type' => 'slideshow']),
      '#attributes' => [
        'target' => '_blank',
        'class' => ['button', 'primary'],
        // Preparation to open the dialog in a modal, but as we can not
        // simply set the created slideshow, we didn't proceed here yet:
        // 'class' => ['button', 'primary', 'use-ajax'],
        // 'data-dialog-type' => 'modal',
        // 'data-dialog-options' => '{width: 90%, height: 80%}',
      ],
      '#weight' => 99,
    ];
  }
}