You are here

public function GroupMenuBlock::getMenuInstance in Group Content Menu 8

Gets the menu instance for the current group.

Return value

\Drupal\group_content_menu\GroupContentMenuInterface|null The instance of the menu or null if no instance is found.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to GroupMenuBlock::getMenuInstance()
GroupMenuBlock::build in src/Plugin/Block/GroupMenuBlock.php
Builds and returns the renderable array for this block plugin.
GroupMenuBlock::getMenuName in src/Plugin/Block/GroupMenuBlock.php
Returns a name for the menu.

File

src/Plugin/Block/GroupMenuBlock.php, line 266

Class

GroupMenuBlock
Provides a generic Menu block.

Namespace

Drupal\group_content_menu\Plugin\Block

Code

public function getMenuInstance() {
  $entity = $this
    ->getContext('group')
    ->getContextData()
    ->getValue();

  // Don't load menu for group entities that are new/unsaved.
  if (!$entity || $entity
    ->isNew()) {
    return NULL;
  }

  /** @var \Drupal\group\Entity\Storage\GroupContentStorage $groupStorage */
  $groupStorage = $this->entityTypeManager
    ->getStorage('group_content');
  $contentPluginId = $groupStorage
    ->loadByContentPluginId($this
    ->getPluginId());
  if (empty($contentPluginId)) {
    return NULL;
  }
  $instances = $groupStorage
    ->loadByGroup($entity, $this
    ->getPluginId());
  if ($instances) {
    return array_pop($instances)
      ->getEntity();
  }
  return NULL;
}