og_sm-8.x-1.0/og_sm_content/og_sm_content.module
og_sm_content/og_sm_content.module
<?php
/**
* @file
* Site content creation and administration functionality.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\og_sm\OgSm;
/**
* Implements hook_theme_registry_alter().
*/
function og_sm_content_theme_registry_alter(&$theme_registry) {
if (!isset($theme_registry['node_add_list'])) {
return;
}
$preprocess_functions = $theme_registry['node_add_list']['preprocess functions'];
$key = array_search('og_sm_content_preprocess_node_add_list', $preprocess_functions);
if ($key !== FALSE) {
unset($preprocess_functions[$key]);
$preprocess_functions[] = 'og_sm_content_preprocess_node_add_list';
$theme_registry['node_add_list']['preprocess functions'] = $preprocess_functions;
}
}
/**
* Implements hook_entity_field_access().
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
function og_sm_content_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
$site = OgSm::siteManager()->currentSite();
if (!$site) {
return AccessResult::neutral();
}
/** @var \Drupal\og\OgAccessInterface $og_access */
$og_access = \Drupal::service('og.access');
// Normally the permission "administer nodes" is required to change the node
// author, options, etc. That global role might not be granted in a site
// context. We add an extra check on the Site role "administer site" to allow
// access to these fields.
$fields = ['uid', 'status', 'created', 'promote', 'sticky'];
if (in_array($field_definition->getName(), $fields, TRUE)) {
if ($operation === 'edit' && $og_access->userAccess($site, 'administer site', $account)->isAllowed()) {
return AccessResult::allowed();
}
return AccessResult::neutral();
}
// Deny access to entity reference fields that require another type of group.
if ($field_definition->getType() === 'entity_reference') {
$handler = $field_definition->getSetting('handler');
$handler_settings = $field_definition->getSetting('handler_settings');
if (strpos($handler, ':') !== FALSE) {
// Extract the entity type from the handler.
$entity_type = substr(strstr($handler, ':'), 1);
try {
// Get all bundles of the referenced entity type.
$bundle_ids = Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type);
if (!$bundle_ids) {
return AccessResult::neutral();
}
$bundle_ids = array_keys($bundle_ids);
// Filter out the bundles that cannot be referenced.
if (!empty($handler_settings['target_bundles'])) {
$bundle_ids = array_intersect($bundle_ids, $handler_settings['target_bundles']);
}
/** @var \Drupal\og\GroupTypeManagerInterface $group_type_manager */
$group_type_manager = Drupal::service('og.group_type_manager');
// Get the group content bundles of $entity_type.
$content_bundle_ids = $group_type_manager->getAllGroupContentBundlesByEntityType($entity_type);
// Return neutral if none of the bundles are group content or
// if at least one none-group content bundle can be referenced.
if (!$content_bundle_ids || array_diff($bundle_ids, $content_bundle_ids)) {
return AccessResult::neutral();
}
// Get the group content bundles of $entity_type that may be referenced
// by the current site.
$content_bundle_ids = $group_type_manager->getGroupContentBundleIdsByGroupBundle($site->getEntityTypeId(), $site->bundle());
$content_bundle_ids = $content_bundle_ids[$entity_type] ?? [];
// Deny access if none of the allowed bundles are listed.
if (!$content_bundle_ids || !array_intersect($bundle_ids, $content_bundle_ids)) {
return AccessResult::forbidden();
}
}
catch (Exception $ex) {
return AccessResult::neutral();
}
}
}
return AccessResult::neutral();
}
/**
* Implements hook_preprocess_HOOK() for list of available node type templates.
*
* Add site context to the node_add links when the theme suggestion
* "node_add_list__og_sm_site" is used.
*/
function og_sm_content_preprocess_node_add_list(&$variables) {
if ($variables['theme_hook_original'] !== 'node_add_list__og_sm_site' || empty($variables['types'])) {
return;
}
$site = OgSm::siteManager()->currentSite();
if (!$site) {
return;
}
/** @var \Drupal\Core\Utility\LinkGeneratorInterface $link_generator */
$link_generator = \Drupal::service('link_generator');
foreach ($variables['content'] as $type) {
/** @var \Drupal\node\NodeTypeInterface $type */
$url = new Url('og_sm.site_content.add', [
'node' => $site->id(),
'node_type' => $type->id(),
]);
$variables['types'][$type->id()]['add_link'] = $link_generator->generate($type->label(), $url);
// Also set the url key since themes like seven have a custom template for
// node_add_list.
$variables['types'][$type->id()]['url'] = $url;
}
}
/**
* Implements hook_entity_type_alter().
*/
function og_sm_content_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
$entity_types['node']->setListBuilderClass('Drupal\og_sm_content\NodeListBuilder');
}
