You are here

public function OgMenuInstanceController::createMenuInstance in Organic Groups Menu (OG Menu) 8

Controller for the create menu instance form.

Depending on whether the menu instance already exists, the user will be redirected to the entity create or edit form.

Parameters

\Drupal\og_menu\Entity\OgMenu $ogmenu: The OG Menu that is associated with the menu instance.

\Drupal\Core\Entity\EntityInterface $og_group: The group that is associated with the menu instance.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse The response.

1 string reference to 'OgMenuInstanceController::createMenuInstance'
og_menu.routing.yml in ./og_menu.routing.yml
og_menu.routing.yml

File

src/Controller/OgMenuInstanceController.php, line 70
Contains Drupal\og_menu\Controller\OgMenuInstanceController.

Class

OgMenuInstanceController
Class OgMenuInstanceController.

Namespace

Drupal\og_menu\Controller

Code

public function createMenuInstance(OgMenu $ogmenu, EntityInterface $og_group) {
  $values = [
    'type' => $ogmenu
      ->id(),
    OgGroupAudienceHelperInterface::DEFAULT_FIELD => $og_group
      ->id(),
  ];

  // Menu exists, redirect to edit form.
  $instances = $this
    ->entityTypeManager()
    ->getStorage('ogmenu_instance')
    ->loadByProperties($values);
  if ($instances) {
    $instance = array_pop($instances);
    return $this
      ->redirect('entity.ogmenu_instance.edit_form', [
      'ogmenu_instance' => $instance
        ->id(),
    ]);
  }

  // Create new menu instance.
  $entity = OgMenuInstance::create($values);
  $entity
    ->save();
  if ($entity
    ->id()) {
    return $this
      ->redirect('entity.ogmenu_instance.edit_form', [
      'ogmenu_instance' => $entity
        ->id(),
    ]);
  }
  throw new Exception('Unable to save menu instance.');
}