From 9eff741b9687d8e1380581585f0a52188eff95d2 Mon Sep 17 00:00:00 2001 From: Dan Braghis Date: Mon, 13 Mar 2017 16:01:28 +0000 Subject: [PATCH] Add access field toggle in the bundle form --- og_access/og_access.info.yml | 3 +- og_access/og_access.module | 54 +++++++++++++---- og_access/src/BundleFormAlter.php | 96 +++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 og_access/src/BundleFormAlter.php diff --git a/og_access/og_access.info.yml b/og_access/og_access.info.yml index 054d6130f..0a1474b89 100644 --- a/og_access/og_access.info.yml +++ b/og_access/og_access.info.yml @@ -1,5 +1,5 @@ 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 @@ -7,3 +7,4 @@ type: module dependencies: - og + - og_ui diff --git a/og_access/og_access.module b/og_access/og_access.module index b1641c9de..66b7f4566 100644 --- a/og_access/og_access.module +++ b/og_access/og_access.module @@ -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. @@ -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(). */ @@ -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; + } } diff --git a/og_access/src/BundleFormAlter.php b/og_access/src/BundleFormAlter.php new file mode 100644 index 000000000..97de0b928 --- /dev/null +++ b/og_access/src/BundleFormAlter.php @@ -0,0 +1,96 @@ +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; + } + +}