View source
<?php
declare (strict_types=1);
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\og\Entity\OgRole;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
use Drupal\og\OgMembershipInterface;
use Drupal\og\OgRoleInterface;
use Drupal\system\Entity\Action;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\UserInterface;
function og_entity_insert(EntityInterface $entity) {
og_invalidate_group_content_cache_tags($entity);
if (!Og::isGroup($entity
->getEntityTypeId(), $entity
->bundle())) {
return;
}
if (!$entity instanceof EntityOwnerInterface) {
return;
}
$owner = $entity
->getOwner();
if (empty($owner) || $owner
->isAnonymous()) {
return;
}
if (!Og::getMembership($entity, $entity
->getOwner(), OgMembershipInterface::ALL_STATES)) {
$membership = Og::createMembership($entity, $entity
->getOwner());
$membership
->save();
}
}
function og_entity_update(EntityInterface $entity) {
og_invalidate_group_content_cache_tags($entity);
}
function og_entity_predelete(EntityInterface $entity) {
if (Og::isGroup($entity
->getEntityTypeId(), $entity
->bundle())) {
$config = \Drupal::config('og.settings');
if ($config
->get('delete_orphans')) {
$plugin_id = $config
->get('delete_orphans_plugin_id');
$plugin = \Drupal::service('plugin.manager.og.delete_orphans')
->createInstance($plugin_id, []);
$plugin
->register($entity);
}
}
if ($entity instanceof UserInterface) {
$membership_manager = \Drupal::service('og.membership_manager');
foreach ($membership_manager
->getMemberships($entity
->id(), []) as $membership) {
$membership
->delete();
}
}
}
function og_entity_delete(EntityInterface $entity) {
og_invalidate_group_content_cache_tags($entity);
if (Og::isGroup($entity
->getEntityTypeId(), $entity
->bundle()) || Og::isGroupContent($entity
->getEntityTypeId(), $entity
->bundle())) {
Og::invalidateCache();
}
if ($entity instanceof ConfigEntityBundleBase) {
$bundle = $entity
->id();
$entity_type_id = \Drupal::entityTypeManager()
->getDefinition($entity
->getEntityTypeId())
->getBundleOf();
if (Og::isGroup($entity_type_id, $bundle)) {
Og::groupTypeManager()
->removeGroup($entity_type_id, $bundle);
}
}
}
function og_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($entity instanceof OgRoleInterface && $operation == 'view') {
return AccessResult::allowed();
}
if (!$entity instanceof ContentEntityInterface) {
return AccessResult::neutral();
}
if ($operation == 'view') {
return AccessResult::neutral();
}
$entity_type_id = $entity
->getEntityTypeId();
$bundle_id = $entity
->bundle();
$is_group_content = Og::isGroupContent($entity_type_id, $bundle_id);
if (!Og::isGroup($entity_type_id, $bundle_id) && !$is_group_content) {
return AccessResult::neutral();
}
if ($is_group_content && \Drupal::service('og.membership_manager')
->getGroupCount($entity) === 0) {
return AccessResult::neutral();
}
if ($account
->hasPermission('administer organic groups')) {
return AccessResult::allowed();
}
$access = \Drupal::service('og.access')
->userAccessEntityOperation($operation, $entity, $account);
if ($access
->isAllowed()) {
return $access;
}
if ($entity_type_id == 'node') {
$node_access_strict = \Drupal::config('og.settings')
->get('node_access_strict');
return AccessResult::forbiddenIf($node_access_strict);
}
return AccessResult::forbidden();
}
function og_entity_create_access(AccountInterface $account, array $context, $bundle) {
$entity_type_id = $context['entity_type_id'];
if (!Og::isGroupContent($entity_type_id, $bundle)) {
return AccessResult::neutral();
}
$access_result = AccessResult::allowedIfHasPermission($account, 'administer organic groups');
if ($access_result
->isAllowed()) {
return $access_result;
}
$node_access_strict = \Drupal::config('og.settings')
->get('node_access_strict');
if ($entity_type_id == 'node' && !$node_access_strict && $account
->hasPermission("create {$bundle} content")) {
return AccessResult::neutral();
}
$required = FALSE;
$field_definitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type_id, $bundle);
foreach ($field_definitions as $field_definition) {
if (!\Drupal::service('og.group_audience_helper')
->isGroupAudienceField($field_definition)) {
continue;
}
$options = [
'target_type' => $field_definition
->getFieldStorageDefinition()
->getSetting('target_type'),
'handler' => $field_definition
->getSetting('handler'),
'field_mode' => 'admin',
];
$handler = \Drupal::service('plugin.manager.entity_reference_selection');
if ($handler
->getInstance($options)) {
return AccessResult::neutral();
}
$required = $field_definition
->isRequired();
}
return $required ? AccessResult::forbiddenIf($node_access_strict) : AccessResult::neutral();
}
function og_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
if (!Og::isGroup($entity_type
->id(), $bundle)) {
return NULL;
}
$fields = [];
$fields['og_group'] = BaseFieldDefinition::create('og_group')
->setLabel(new TranslatableMarkup('OG Group'))
->setComputed(TRUE)
->setTranslatable(FALSE)
->setDefaultValue(TRUE)
->setReadOnly(TRUE);
return $fields;
}
function og_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
if (!isset($fields['og_group'])) {
return;
}
$fields['og_group']
->setDisplayOptions('view', [
'weight' => 0,
'type' => 'og_group_subscribe',
])
->setDisplayConfigurable('view', TRUE);
}
function og_field_formatter_info_alter(array &$info) {
foreach (array_keys($info) as $key) {
if (!in_array('entity_reference', $info[$key]['field_types'])) {
continue;
}
$info[$key]['field_types'][] = OgGroupAudienceHelperInterface::GROUP_REFERENCE;
}
}
function og_field_widget_info_alter(array &$info) {
$info['options_buttons']['field_types'][] = OgGroupAudienceHelperInterface::GROUP_REFERENCE;
}
function og_entity_type_alter(array &$entity_types) {
foreach ($entity_types as $entity_type_id => $entity_type) {
$entity_type
->setLinkTemplate('og-admin-routes', "/group/{$entity_type_id}/{{$entity_type_id}}/admin");
}
}
function og_theme($existing, $type, $theme, $path) {
return [
'og_member_count' => [
'variables' => [
'count' => 0,
'membership_states' => [],
'group' => NULL,
'group_label' => NULL,
],
],
];
}
function og_invalidate_group_content_cache_tags(EntityInterface $entity) {
$is_group_content = Og::isGroupContent($entity
->getEntityTypeId(), $entity
->bundle());
if ($is_group_content) {
$membership_manager = \Drupal::service('og.membership_manager');
$tags = [];
$original = !empty($entity->original) ? $entity->original : NULL;
if ($original) {
$group_audience_helper = \Drupal::service('og.group_audience_helper');
foreach ($group_audience_helper
->getAllGroupAudienceFields($entity
->getEntityTypeId(), $entity
->bundle()) as $field) {
$field_name = $field
->getName();
$original_field_item_list = $original
->get($field_name);
if (!$entity
->get($field_name)
->equals($original_field_item_list)) {
foreach ($original_field_item_list
->referencedEntities() as $old_group) {
$tags = Cache::mergeTags($tags, $old_group
->getCacheTagsToInvalidate());
}
}
}
}
foreach ($membership_manager
->getGroups($entity) as $groups) {
foreach ($groups as $group) {
$tags = Cache::mergeTags($tags, $group
->getCacheTagsToInvalidate());
}
}
Cache::invalidateTags(Cache::buildTags('og-group-content', $tags));
}
}
function og_og_role_insert(OgRoleInterface $role) {
if ($role
->getRoleType() === OgRoleInterface::ROLE_TYPE_REQUIRED) {
return;
}
if ($role
->isSyncing()) {
return;
}
$add_id = 'og_membership_add_single_role_action.' . $role
->getName();
if (!Action::load($add_id)) {
$action = Action::create([
'id' => $add_id,
'type' => 'og_membership',
'label' => new TranslatableMarkup('Add the @label role to the selected members', [
'@label' => $role
->getName(),
]),
'configuration' => [
'role_name' => $role
->getName(),
],
'plugin' => 'og_membership_add_single_role_action',
]);
$action
->trustData()
->save();
}
$remove_id = 'og_membership_remove_single_role_action.' . $role
->getName();
if (!Action::load($remove_id)) {
$action = Action::create([
'id' => $remove_id,
'type' => 'og_membership',
'label' => new TranslatableMarkup('Remove the @label role from the selected members', [
'@label' => $role
->getName(),
]),
'configuration' => [
'role_name' => $role
->getName(),
],
'plugin' => 'og_membership_remove_single_role_action',
]);
$action
->trustData()
->save();
}
}
function og_og_role_delete(OgRoleInterface $role) {
$role_name = $role
->getName();
$actions = Action::loadMultiple([
'og_membership_add_single_role_action.' . $role_name,
'og_membership_remove_single_role_action.' . $role_name,
]);
foreach (OgRole::loadMultiple() as $role) {
if ($role
->getName() === $role_name) {
return;
}
}
foreach ($actions as $action) {
$action
->delete();
}
}