View source
<?php
namespace Drupal\photos\Form;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\media_library\Form\AddFormBase;
use Drupal\photos\Plugin\media\Source\Photos;
class PhotosMediaLibraryForm extends AddFormBase {
public function getFormId() {
return $this
->getBaseFormId() . '_photos';
}
protected function getMediaType(FormStateInterface $form_state) {
if ($this->mediaType) {
return $this->mediaType;
}
$media_type = parent::getMediaType($form_state);
if ($media_type && !$media_type
->getSource() instanceof Photos) {
throw new \InvalidArgumentException('Can only add media types which use a Photos source plugin.');
}
return $media_type;
}
protected function buildInputElement(array $form, FormStateInterface $form_state) {
$media_type = $this
->getMediaType($form_state);
$form['container'] = [
'#type' => 'container',
];
$form['container']['photos'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'node',
'#title' => $this
->t('Photo album'),
'#description' => $this
->t('Photo album node.'),
'#required' => TRUE,
'#selection_handler' => 'default',
'#selection_settings' => [
'target_bundles' => [
'photos',
],
],
];
$form['container']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Add'),
'#button_type' => 'primary',
'#validate' => [
'::validateUrl',
],
'#submit' => [
'::addButtonSubmit',
],
'#ajax' => [
'callback' => '::updateFormCallback',
'wrapper' => 'media-library-wrapper',
'url' => Url::fromRoute('media_library.ui'),
'options' => [
'query' => $this
->getMediaLibraryState($form_state)
->all() + [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
],
],
];
return $form;
}
public function addButtonSubmit(array $form, FormStateInterface $form_state) {
$this
->processInputValues([
$form_state
->getValue('photos'),
], $form, $form_state);
}
}