Created
November 28, 2018 22:19
-
-
Save jackbravo/3e3ab5467aed53f371683f2ead5b052c to your computer and use it in GitHub Desktop.
Example to launch media library with a custom input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\layout_manager\Controller; | |
use Drupal\Core\Controller\ControllerBase; | |
use Drupal\Core\Field\FieldStorageDefinitionInterface; | |
use Drupal\Core\Url; | |
/** | |
* Controller to render basic html for client side application. | |
*/ | |
class MediaLibraryController extends ControllerBase { | |
/** | |
* Action to render basic html. | |
* | |
* @return array | |
* The `render array` content from template. | |
* | |
* @codeCoverageIgnore | |
* Ignored because this is just returning a specific HTML payload that the app | |
* can render on -- nothing to test. | |
*/ | |
public function content() { | |
$widget_id = 'custom_widget'; | |
return [ | |
'media_library_open_button' => [ | |
'#type' => 'link', | |
'#title' => $this->t('Browse media'), | |
'#name' => 'chip-media-open-library-button', | |
// @todo Make the view configurable in https://www.drupal.org/project/drupal/issues/2971209 | |
'#url' => Url::fromRoute('view.media_library.widget', [], [ | |
'query' => [ | |
'media_library_widget_id' => $widget_id, | |
'media_library_allowed_types' => ['distribution_image', 'ngp_image'], | |
'media_library_remaining' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, | |
], | |
]), | |
'#attributes' => [ | |
'class' => ['btn', 'button', 'use-ajax', 'media-library-open-button'], | |
'data-dialog-type' => 'modal', | |
'data-dialog-options' => json_encode([ | |
'dialogClass' => 'media-library-widget-modal', | |
'height' => '75%', | |
'width' => '75%', | |
'title' => $this->t('Media library'), | |
]), | |
], | |
], | |
// This hidden field and button are used to add new items to the widget. | |
'media_library_selection' => [ | |
'#type' => 'textfield', | |
'#attributes' => [ | |
// This is used to pass the selection from the modal to the widget. | |
'data-media-library-widget-value' => $widget_id, | |
], | |
], | |
// When a selection is made this hidden button is pressed to add new media | |
// items based on the "media_library_selection" value. | |
'media_library_update_widget' => [ | |
'#type' => 'submit', | |
'#value' => $this->t('Update widget'), | |
'#name' => 'media-library-update', | |
/* | |
'#ajax' => [ | |
'callback' => [static::class, 'updateWidget'], | |
], | |
*/ | |
'#attributes' => [ | |
'data-media-library-widget-update' => $widget_id, | |
/* 'class' => ['js-hide'], */ | |
], | |
/* | |
'#validate' => [[static::class, 'validateItems']], | |
'#submit' => [[static::class, 'updateItems']], | |
*/ | |
], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment