You are here

class SubgroupWizardController in Subgroup (Graph) 1.0.x

Returns responses for 'subgroup' GroupContent routes.

Hierarchy

Expanded class hierarchy of SubgroupWizardController

File

src/Controller/SubgroupWizardController.php, line 23

Namespace

Drupal\ggroup\Controller
View source
class SubgroupWizardController extends ControllerBase {

  /**
   * The private store for temporary subgroups.
   *
   * @var \Drupal\user\privateTempStoreFactory
   */
  protected $privateTempStoreFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity form builder.
   *
   * @var \Drupal\Core\Entity\EntityFormBuilderInterface
   */
  protected $entityFormBuilder;

  /**
   * The group content plugin manager.
   *
   * @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface
   */
  protected $pluginManager;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new SubgroupWizardController.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The factory for the temp store object.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
   *   The entity form builder.
   * @param \Drupal\group\Plugin\GroupContentEnablerManagerInterface $plugin_manager
   *   The group content plugin manager.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager, EntityFormBuilderInterface $entity_form_builder, GroupContentEnablerManagerInterface $plugin_manager, RendererInterface $renderer) {
    $this->privateTempStoreFactory = $temp_store_factory
      ->get('ggroup_add_temp');
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFormBuilder = $entity_form_builder;
    $this->pluginManager = $plugin_manager;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('tempstore.private'), $container
      ->get('entity_type.manager'), $container
      ->get('entity.form_builder'), $container
      ->get('plugin.manager.group_content_enabler'), $container
      ->get('renderer'));
  }

  /**
   * Provides the form for creating a subgroup in a group.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group to create a subgroup in.
   * @param \Drupal\group\Entity\GroupTypeInterface $group_type
   *   The subgroup type to create.
   *
   * @return array
   *   The form array for either step 1 or 2 of the subgroup creation wizard.
   */
  public function addForm(GroupInterface $group, GroupTypeInterface $group_type) {
    $plugin_id = "subgroup:{$group_type->id()}";
    $storage_id = "{$plugin_id}:{$group->id()}";
    $creation_wizard = $group
      ->getGroupType()
      ->getContentPlugin($plugin_id)
      ->getConfiguration()['use_creation_wizard'];

    // If we are on step one, we need to build a group form.
    if ($this->privateTempStoreFactory
      ->get("{$storage_id}:step") !== 2) {
      $this->privateTempStoreFactory
        ->set("{$storage_id}:step", 1);

      // Only create a new group if we have nothing stored.
      if (!($entity = $this->privateTempStoreFactory
        ->get("{$storage_id}:group"))) {
        $entity = Group::create([
          'type' => $group_type
            ->id(),
        ]);
      }
    }
    else {

      /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */
      $plugin = $group
        ->getGroupType()
        ->getContentPlugin($plugin_id);
      $entity = GroupContent::create([
        'type' => $plugin
          ->getContentTypeConfigId(),
        'gid' => $group
          ->id(),
      ]);
      if (!$creation_wizard && ($entity = $this->privateTempStoreFactory
        ->get("{$storage_id}:group"))) {
        $entity
          ->save();
        $group
          ->addContent($entity, $plugin_id);

        // We also clear the private store so we can start fresh next time
        // around.
        $this->privateTempStoreFactory
          ->delete("{$storage_id}:step");
        $this->privateTempStoreFactory
          ->delete("{$storage_id}:group");
        return $this
          ->redirect('entity.group.canonical', [
          'group' => $entity
            ->id(),
        ]);
      }
    }

    // Return the form with the group and storage ID added to the form state.
    $extra = [
      'group' => $group,
      'storage_id' => $storage_id,
      'wizard' => $creation_wizard,
    ];
    return $this
      ->entityFormBuilder()
      ->getForm($entity, 'ggroup-form', $extra);
  }

  /**
   * The _title_callback for the add group form route.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group to create a group in.
   * @param \Drupal\group\Entity\GroupTypeInterface $group_type
   *   The group type to create.
   *
   * @return string
   *   The page title.
   */
  public function addFormTitle(GroupInterface $group, GroupTypeInterface $group_type) {
    return $this
      ->t('Create %type in %label', [
      '%type' => $group_type
        ->label(),
      '%label' => $group
        ->label(),
    ]);
  }

  /**
   * Provides the subgroup creation overview page.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group to add the subgroup to.
   *
   * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
   *   The subgroup creation overview page or a redirect to the create form if
   *   we only have 1 bundle.
   */
  public function addPage(GroupInterface $group) {

    // We do not set the "entity_add_list" template's "#add_bundle_message" key
    // because we deny access to the page if no bundle is available.
    $build = [
      '#theme' => 'entity_add_list',
      '#bundles' => [],
    ];
    $add_form_route = 'entity.group_content.subgroup_add_form';

    // Retrieve all subgroup plugins for the group's type.
    $plugin_ids = $this->pluginManager
      ->getInstalledIds($group
      ->getGroupType());
    foreach ($plugin_ids as $key => $plugin_id) {
      if (strpos($plugin_id, 'subgroup:') !== 0) {
        unset($plugin_ids[$key]);
      }
    }
    $storage = $this->entityTypeManager
      ->getStorage('group_content_type');
    $properties = [
      'group_type' => $group
        ->bundle(),
      'content_plugin' => $plugin_ids,
    ];

    /** @var \Drupal\group\Entity\GroupContentTypeInterface[] $bundles */
    $bundles = $storage
      ->loadByProperties($properties);

    // Filter out the bundles the user doesn't have access to.
    $access_control_handler = $this->entityTypeManager
      ->getAccessControlHandler('group_content');
    foreach (array_keys($bundles) as $bundle) {

      // Check for access and add it as a cacheable dependency.
      $access = $access_control_handler
        ->createAccess($bundle, NULL, [
        'group' => $group,
      ], TRUE);
      $this->renderer
        ->addCacheableDependency($build, $access);

      // Remove inaccessible bundles from the list.
      if (!$access
        ->isAllowed()) {
        unset($bundles[$bundle]);
      }
    }

    // Redirect if there's only one bundle available.
    if (count($bundles) == 1) {
      $group_content_type = reset($bundles);
      $plugin = $group_content_type
        ->getContentPlugin();
      $route_params = [
        'group' => $group
          ->id(),
        'group_type' => $plugin
          ->getEntityBundle(),
      ];
      $url = Url::fromRoute($add_form_route, $route_params, [
        'absolute' => TRUE,
      ]);
      return new RedirectResponse($url
        ->toString());
    }

    // Get the subgroup type storage handler.
    $storage_handler = $this->entityTypeManager
      ->getStorage('group_type');

    // Set the info for all of the remaining bundles.
    foreach ($bundles as $bundle => $group_content_type) {
      $plugin = $group_content_type
        ->getContentPlugin();
      $bundle_label = $storage_handler
        ->load($plugin
        ->getEntityBundle())
        ->label();
      $route_params = [
        'group' => $group
          ->id(),
        'group_type' => $plugin
          ->getEntityBundle(),
      ];
      $build['#bundles'][$bundle] = [
        'label' => $bundle_label,
        'description' => $this
          ->t('Create a subgroup of type %group_type for the group.', [
          '%group_type' => $bundle_label,
        ]),
        'add_link' => Link::createFromRoute($bundle_label, $add_form_route, $route_params),
      ];
    }

    // Add the list cache tags for the GroupContentType entity type.
    $bundle_entity_type = $this->entityTypeManager
      ->getDefinition('group_content_type');
    $build['#cache']['tags'] = $bundle_entity_type
      ->getListCacheTags();
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
SubgroupWizardController::$entityFormBuilder protected property The entity form builder. Overrides ControllerBase::$entityFormBuilder
SubgroupWizardController::$entityTypeManager protected property The entity type manager. Overrides ControllerBase::$entityTypeManager
SubgroupWizardController::$pluginManager protected property The group content plugin manager.
SubgroupWizardController::$privateTempStoreFactory protected property The private store for temporary subgroups.
SubgroupWizardController::$renderer protected property The renderer.
SubgroupWizardController::addForm public function Provides the form for creating a subgroup in a group.
SubgroupWizardController::addFormTitle public function The _title_callback for the add group form route.
SubgroupWizardController::addPage public function Provides the subgroup creation overview page.
SubgroupWizardController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
SubgroupWizardController::__construct public function Constructs a new SubgroupWizardController.