sites_group_overrides-1.x-dev/modules/sites_group_media/src/Form/MediaLibraryFormTrait.php
modules/sites_group_media/src/Form/MediaLibraryFormTrait.php
<?php
namespace Drupal\sites_group_media\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\sites_group\Plugin\Site\GroupSiteInterface;
/**
* Creates a form to create media entities from uploaded files.
*/
trait MediaLibraryFormTrait {
/**
* {@inheritdoc}
*/
protected function getMediaLibraryState(FormStateInterface $form_state) {
$state = parent::getMediaLibraryState($form_state);
// Store the media item in state so we can use it even if it's not added to
// the group yet.
if (!empty($form_state->get('media'))) {
foreach ($form_state->get('media') as $media) {
if (!$media->isNew()) {
$state->set('media', $media->id());
}
}
}
// If we create the media in the context of a sites group, save the group
// context, so we can use this information in the media library process.
/** @var \Drupal\sites\SiteInterface $site */
if ($site = \Drupal::service('sites.current_site')->getSiteFromContext()) {
if ($site instanceof GroupSiteInterface) {
$group = $site->getGroup();
$state->set('group', $group->id());
}
}
return $state;
}
/**
* Gets the route match.
*
* @return \Drupal\Core\Routing\RouteMatchInterface
*/
protected function getRouteMatch() {
if (!$this->routeMatch) {
$this->routeMatch = \Drupal::routeMatch();
}
return $this->routeMatch;
}
}
