You are here

public function OgMenuInstance::getGroup in Organic Groups Menu (OG Menu) 8

Returns the group that is associated with the menu instance.

Return value

\Drupal\Core\Entity\EntityInterface The group entity.

Throws

\Exception Thrown when no group is associated, or the group could not be retrieved.

Overrides OgMenuInstanceInterface::getGroup

File

src/Entity/OgMenuInstance.php, line 140
Contains \Drupal\og_menu\Entity\OgMenuInstance.

Class

OgMenuInstance
Defines the OG Menu instance entity.

Namespace

Drupal\og_menu\Entity

Code

public function getGroup() {
  $field_storage = FieldStorageConfig::loadByName($this
    ->getEntityTypeId(), OgGroupAudienceHelperInterface::DEFAULT_FIELD);
  $target_type = $field_storage
    ->getSetting('target_type');
  $value = $this
    ->get(OgGroupAudienceHelperInterface::DEFAULT_FIELD)
    ->getValue();
  if (!$value || !$target_type) {
    throw new \Exception('No group has been associated with the OG Menu instance.');
  }

  /** @var \Drupal\Core\Entity\EntityInterface $target_entity */
  $target_entity = $this
    ->entityTypeManager()
    ->getStorage($target_type)
    ->load($value[0]['target_id']);
  if (!$target_entity) {
    throw new \Exception('The group associated with the OG Menu instance could not be loaded.');
  }
  return $target_entity;
}