buildUi example


  public static function openMediaLibrary(array $form, FormStateInterface $form_state) {
    $triggering_element = $form_state->getTriggeringElement();
    $library_ui = \Drupal::service('media_library.ui_builder')->buildUi($triggering_element['#media_library_state']);
    $dialog_options = MediaLibraryUiBuilder::dialogOptions();
    return (new AjaxResponse())
      ->addCommand(new OpenModalDialogCommand($dialog_options['title']$library_ui$dialog_options));
  }

  /** * Validates that newly selected items can be added to the widget. * * Making an invalid selection from the view should not be possible, but we * still validate in case other selection methods (ex: upload) are valid. * * @param array $form * The form array. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. */
/** * Build the media library UI for a selected type. * * @param string $selected_type_id * The selected media type ID. * * @return array * The render array for the media library. */
  protected function buildLibraryUi($selected_type_id) {
    $state = MediaLibraryState::create('test', ['image', 'remote_video']$selected_type_id, -1);
    return \Drupal::service('media_library.ui_builder')->buildUi($state);
  }

  /** * Tests the validation of the library state in the media library add form. */
  public function testFormStateValidation() {
    $form_state = new FormState();
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('The media library state is not present in the form state.');
    \Drupal::formBuilder()->buildForm(FileUploadForm::class$form_state);
  }

  
$this->createMediaType('image', ['id' => 'deny_access'])->id(),
      $this->createMediaType('image')->id(),
    ];

    $account = $this->createUser(['create media']);
    $this->setCurrentUser($account);

    /** @var \Drupal\media_library\MediaLibraryUiBuilder $ui_builder */
    $ui_builder = $this->container->get('media_library.ui_builder');

    $state = MediaLibraryState::create('test', $media_types$media_types[0], 1);
    $build = $ui_builder->buildUi($state);
    $this->assertEmpty($build['content']['form']);

    $state = MediaLibraryState::create('test', $media_types$media_types[1], 1);
    $build = $ui_builder->buildUi($state);
    $this->assertNotEmpty($build['content']['form']);
  }

  /** * Asserts various aspects of an access result. * * @param \Drupal\Core\Access\AccessResult $access_result * The access result. * @param bool $is_allowed * The expected access status. * @param string $expected_reason * (optional) The expected reason attached to the access result. * @param string[] $expected_cache_tags * (optional) The expected cache tags attached to the access result. * @param string[] $expected_cache_contexts * (optional) The expected cache contexts attached to the access result. */

  protected function buildMediaLibraryUi(FormStateInterface $form_state) {
    // Get the render array for the media library. The media library state might     // contain the 'media_library_content' when it has been opened from a     // vertical tab. We need to remove that to make sure the render array     // contains the vertical tabs. Besides that, we also need to force the media     // library to create a new instance of the media add form.     // @see \Drupal\media_library\MediaLibraryUiBuilder::buildMediaTypeAddForm()     $state = $this->getMediaLibraryState($form_state);
    $state->remove('media_library_content');
    $state->set('_media_library_form_rebuild', TRUE);
    return $this->libraryUiBuilder->buildUi($state);
  }

  /** * AJAX callback to send the new media item(s) to the calling code. * * @param array $form * The complete form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current form state. * * @return array|\Drupal\Core\Ajax\AjaxResponse * The form array when there are form errors or an AJAX response to select * the created items in the media library. */
Home | Imprint | This part of the site doesn't use cookies.