Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
Add access field toggle in the bundle form
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Mar 13, 2017
1 parent 09a553d commit 444bc1a
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 12 deletions.
3 changes: 2 additions & 1 deletion og_access/og_access.info.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Organic Groups access control
description: Enable access control for private and public groups and group content."
description: "Enable access control for private and public groups and group content."
package: Organic Groups

core: 8.x
type: module

dependencies:
- og
- og_ui
54 changes: 43 additions & 11 deletions og_access/og_access.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
*/

use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\NodeInterface;
use Drupal\og\Og;
use Drupal\og_access\BundleFormAlter;

/**
* The access realm of group member.
Expand Down Expand Up @@ -152,6 +155,16 @@ function og_access_node_access_records(NodeInterface $node) {
return !empty($grants) ? $grants : [];
}

/**
* Implements hook_form_alter().
*/
function og_access_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
if ($form_state->getFormObject() instanceof BundleEntityFormBase) {
(new BundleFormAlter($form_state->getFormObject()->getEntity()))
->formAlter($form, $form_state);
}
}

/**
* Implements hook_entity_insert().
*/
Expand Down Expand Up @@ -181,28 +194,47 @@ function og_access_entity_type_save(EntityInterface $entity) {
$definition = \Drupal::entityTypeManager()->getDefinition($entity->getEntityTypeId());
$entity_type_id = $definition->getBundleOf();

$enable_og_access = $entity->og_enable_access;

// Add/remove on the group itself.
$is_group = Og::isGroup($entity_type_id, $bundle);
if ($entity->og_is_group != $is_group) {
if ($entity->og_is_group) {
if ($entity->og_is_group || $is_group) {
$field = FieldConfig::loadByName($entity_type_id, $bundle, OG_ACCESS_FIELD);
if (!$field && $enable_og_access) {
Og::createField(OG_ACCESS_FIELD, $entity_type_id, $bundle);
}
elseif ($field = FieldConfig::loadByName($entity_type_id, $bundle, OG_ACCESS_FIELD)) {
$field->delete();
return;
elseif ($field) {
if (!$enable_og_access || $is_group && !$entity->og_is_group) {
$field->delete();
}
}
}

// Add remove the relevant field.
// Add remove the relevant field to the group content bundle.
$is_group_content = Og::isGroupContent($entity_type_id, $bundle);
if ($entity->og_group_content_bundle != $is_group_content) {
if ($entity->og_group_content_bundle) {
if ($entity->og_group_content_bundle || $is_group_content) {
$field = FieldConfig::loadByName($entity_type_id, $bundle, OG_CONTENT_ACCESS_FIELD);

if (!$field && $enable_og_access) {
Og::createField(OG_CONTENT_ACCESS_FIELD, $entity_type_id, $bundle);
}
elseif ($field = FieldConfig::loadByName($entity_type_id, $bundle, OG_CONTENT_ACCESS_FIELD)) {
$field->delete();
return;
elseif ($field) {
if (!$enable_og_access || $is_group_content && !$entity->og_group_content_bundle) {
$field->delete();
}
}
}
}

/**
* Implements hook_module_implements_alter().
*/
function og_access_module_implements_alter(&$implementations, $hook) {
if ($hook == 'form_alter') {
// Move our form alter after the og_ui one.
// @TODO remove once og_ui and og are merged.
$group = $implementations['og_access'];
unset($implementations['og_access']);
$implementations['og_access'] = $group;
}
}
96 changes: 96 additions & 0 deletions og_access/src/BundleFormAlter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Drupal\og_access;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\og\Og;

/**
* Helper for og_access_form_alter().
*/
class BundleFormAlter {

/**
* The entity bundle.
*
* @var string
*/
protected $bundle;

/**
* The entity type ID.
*
* @var string
*/
protected $entityTypeId;

/**
* The form entity which has been used for populating form element defaults.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;

/**
* Construct a BundleFormAlter object.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity object.
*/
public function __construct(EntityInterface $entity) {
$this->entity = $entity;
}

/**
* This is a helper for og_ui_form_alter().
*
* @param array $form
* The form variable.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
public function formAlter(array &$form, FormStateInterface $form_state) {
// Example: node.
$this->entityTypeId = $this->entity->getEntityType()->getBundleOf();

// Example: article.
$this->bundle = $this->entity->id();

$form['og']['og_enable_access'] = [
'#type' => 'checkbox',
'#title' => t('Enable OG access control'),
'#default_value' => $this->hasAccessControl(),
'#states' => [
'visible' => [
[':input[name="og_is_group"]' => ['checked' => TRUE]],
[':input[name="og_group_content_bundle"]' => ['checked' => TRUE]],
]
],
];
}

/**
* Checks whether the existing bundle has OG access control enabled.
*
* @return bool
* True if the group bundle has the OG_ACCESS_FIELD field -OR-
* if the group content bundle has the OG_CONTENT_ACCESS_FIELD field.
* False otherwise.
*/
protected function hasAccessControl() {
$field_definitions = \Drupal::service('entity_field.manager')
->getFieldDefinitions($this->entityTypeId, $this->bundle);

if (Og::isGroup($this->entityTypeId, $this->bundle)) {
return isset($field_definitions[OG_ACCESS_FIELD]);
}

if (Og::isGroupContent($this->entityTypeId, $this->bundle)) {
return isset($field_definitions[OG_CONTENT_ACCESS_FIELD]);
}

return FALSE;
}

}

0 comments on commit 444bc1a

Please sign in to comment.