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

Refactor \Drupal\og_ui\BundleFormAlter #734

Merged
merged 21 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7e1790c
Og::isGroup() accepts only strings as arguments.
pfrenssen Dec 18, 2021
6cec4d8
Rename the class to make it more clear we are altering forms based on…
pfrenssen Dec 18, 2021
35abf22
Ensure that only entity bundle forms will be altered by this class.
pfrenssen Dec 18, 2021
a681694
Update AJAX test to an actual functional JS test.
pfrenssen Dec 18, 2021
68cfe7c
Turn ::addGroupType() into a static method.
pfrenssen Dec 18, 2021
669b913
Turn ::addGroupContent() into a static method.
pfrenssen Dec 19, 2021
ffaeb6f
Turn ::prepare() into a static method.
pfrenssen Dec 19, 2021
765cce6
Add missing type hints.
pfrenssen Dec 19, 2021
a2a4f62
Enable chromedriver on Travis CI.
pfrenssen Dec 19, 2021
9ca1125
Turn the BundleEntityFormAlter class into a proper service.
pfrenssen Dec 19, 2021
ec73e8c
Adhere to coding standards.
pfrenssen Dec 19, 2021
17ed5d5
Revert "Og::isGroup() accepts only strings as arguments."
pfrenssen Dec 19, 2021
50bdcab
Merge remote-tracking branch 'origin/8.x-1.x' into bundleformalter-wr…
pfrenssen Dec 19, 2021
5f29c96
Adhere to coding standards.
pfrenssen Dec 19, 2021
314f262
Merge remote-tracking branch 'origin/8.x-1.x' into bundleformalter-wr…
pfrenssen Jan 12, 2022
3851cf0
Merge remote-tracking branch 'origin/8.x-1.x' into bundleformalter-wr…
bboro Jul 15, 2024
6de7132
Fix test
bboro Jul 15, 2024
c624d78
Update phpcs config
bboro Jul 18, 2024
c783d05
Linting
bboro Jul 18, 2024
976eb56
Remove spaces around strict types
bboro Jul 19, 2024
370311e
Prefix trigger_error with @
bboro Jul 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ mysql:
services:
- mysql

addons:
chrome: stable

cache:
directories:
- $HOME/.composer/cache/files
Expand Down Expand Up @@ -82,4 +85,7 @@ before_script:
# Export database variable for kernel tests.
- export SIMPLETEST_DB=mysql://root:@127.0.0.1/og

# Start chromedriver for JS tests.
- chromedriver --port=4444 &

script: DRUPAL_DIR=$DRUPAL_DIR MODULE_DIR=$MODULE_DIR $MODULE_DIR/scripts/travis-ci/run-test.sh $TEST_SUITE
2 changes: 1 addition & 1 deletion og.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Hooks provided by the Organic Groups module.
*/

declare(strict_types = 1);
declare(strict_types=1);

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityPublishedInterface;
Expand Down
2 changes: 1 addition & 1 deletion og.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Install, update, and uninstall functions for the Organic groups module.
*/

declare(strict_types = 1);
declare(strict_types=1);

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
Expand Down
2 changes: 1 addition & 1 deletion og.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Enable users to create and manage groups with roles and permissions.
*/

declare(strict_types = 1);
declare(strict_types=1);

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
Expand Down
2 changes: 1 addition & 1 deletion og.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Post-update functions for the Organic groups module.
*/

declare(strict_types = 1);
declare(strict_types=1);

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
Expand Down
2 changes: 1 addition & 1 deletion og.views.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Integration with the Views module.
*/

declare(strict_types = 1);
declare(strict_types=1);

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\FieldStorageConfigInterface;
Expand Down
5 changes: 2 additions & 3 deletions og_ui/og_ui.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Main functions and hook implementations of the Organic Groups UI module.
*/

declare(strict_types = 1);
declare(strict_types=1);

use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\BundleEntityFormBase;
Expand All @@ -15,7 +15,6 @@ use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
use Drupal\og_ui\BundleFormAlter;

/**
* Implements hook_form_alter().
Expand All @@ -31,7 +30,7 @@ function og_ui_form_alter(array &$form, FormStateInterface $form_state, $form_id
return;
}

(new BundleFormAlter($entity_type))->formAlter($form, $form_state);
\Drupal::service('og_ui.bundle_entity_form_alter')->formAlter($form, $form_state);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions og_ui/og_ui.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
og_ui.bundle_entity_form_alter:
class: Drupal\og_ui\BundleEntityFormAlter
arguments: ['@entity_type.manager', '@entity_type.bundle.info', '@og.group_type_manager']
138 changes: 73 additions & 65 deletions og_ui/src/BundleFormAlter.php → og_ui/src/BundleEntityFormAlter.php
Original file line number Diff line number Diff line change
@@ -1,85 +1,84 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\og_ui;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionableEntityBundleInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\og\Og;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\OgGroupAudienceHelperInterface;

/**
* Helper for og_ui_form_alter().
*/
class BundleFormAlter {
class BundleEntityFormAlter {

/**
* Entity type definition.
* The entity type manager.
*
* @var \Drupal\Core\Entity\ContentEntityTypeInterface
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $definition;
protected EntityTypeManagerInterface $entityTypeManager;

/**
* The entity bundle.
* The entity type bundle info service.
*
* @var string
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $bundle;
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo;

/**
* The bundle label.
* The group type manager.
*
* @var string
* @var \Drupal\og\GroupTypeManagerInterface
*/
protected $bundleLabel;
protected GroupTypeManagerInterface $groupTypeManager;

/**
* The entity type ID.
* Constructs a new BundleEntityFormAlter service.
*
* @var string
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
* The entity type bundle info service.
* @param \Drupal\og\GroupTypeManagerInterface $groupTypeManager
* The group type manager.
*/
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;
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo, GroupTypeManagerInterface $groupTypeManager) {
$this->entityTypeManager = $entityTypeManager;
$this->entityTypeBundleInfo = $entityTypeBundleInfo;
$this->groupTypeManager = $groupTypeManager;
}

/**
* This is a helper for og_ui_form_alter().
* Alters bundle entity forms.
*
* @param array $form
* The form variable.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*
* @see og_ui_form_alter()
*/
public function formAlter(array &$form, FormStateInterface $form_state) {
$this->prepare($form, $form_state);
public function formAlter(array &$form, FormStateInterface $form_state): void {
if (!$form_state->getFormObject() instanceof BundleEntityFormBase) {
throw new \InvalidArgumentException('Passed in form is not a bundle entity form.');
}
static::prepare($form);
$this->addGroupType($form, $form_state);
$this->addGroupContent($form, $form_state);
}

/**
* AJAX callback displaying the target bundles select box.
*/
public function ajaxCallback(array $form, FormStateInterface $form_state) {
public static function ajaxCallback(array $form, FormStateInterface $form_state): array {
return $form['og']['og_target_bundles'];
}

Expand All @@ -88,18 +87,8 @@ public function ajaxCallback(array $form, FormStateInterface $form_state) {
*
* @param array $form
* The form variable.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
protected function prepare(array &$form, FormStateInterface $form_state) {
// Example: article.
$this->bundle = $this->entity->id();
// Example: Article.
$this->bundleLabel = Unicode::lcfirst((string) $this->entity->label());
$this->definition = $this->entity->getEntityType();
// Example: node.
$this->entityTypeId = $this->definition->getBundleOf();

protected static function prepare(array &$form): void {
$form['og'] = [
'#type' => 'details',
'#title' => new TranslatableMarkup('Organic groups'),
Expand All @@ -110,48 +99,54 @@ protected function prepare(array &$form, FormStateInterface $form_state) {
}

/**
* Adds the "is group?" checkbox.
* Adds the section to mark the entity type as a group type.
*/
protected function addGroupType(array &$form, FormStateInterface $form_state) {
if ($this->entity->isNew()) {
protected function addGroupType(array &$form, FormStateInterface $form_state): void {
$bundle = static::getEntityBundle($form_state);
if ($bundle->isNew()) {
$description = new TranslatableMarkup('Every entity in this bundle is a group which can contain entities and can have members.');
$default_value = FALSE;
}
else {
$description = new TranslatableMarkup('Every "%bundle" is a group which can contain entities and can have members.', [
'%bundle' => Unicode::lcfirst($this->bundleLabel),
'%bundle' => $bundle->label(),
]);
$default_value = $this->groupTypeManager->isGroup($bundle->getEntityType()->getBundleOf(), $bundle->id());
}
$form['og']['og_is_group'] = [
'#type' => 'checkbox',
'#title' => new TranslatableMarkup('Group'),
'#default_value' => Og::isGroup($this->entityTypeId, $this->bundle),
'#default_value' => $default_value,
'#description' => $description,
];
}

/**
* Adds the "is group content?" checkbox and target settings elements.
* Adds the section to configure the entity type as group content.
*/
protected function addGroupContent(array &$form, FormStateInterface $form_state) {
protected function addGroupContent(array &$form, FormStateInterface $form_state): void {
$bundle = static::getEntityBundle($form_state);
$entity_type_id = $bundle->getEntityType()->getBundleOf();

// Get the stored config from the default group audience field if it exists.
$field = FieldConfig::loadByName($this->entityTypeId, $this->bundle, OgGroupAudienceHelperInterface::DEFAULT_FIELD);
$field = FieldConfig::loadByName($entity_type_id, $bundle->id(), OgGroupAudienceHelperInterface::DEFAULT_FIELD);
$handler_settings = $field ? $field->getSetting('handler_settings') : [];

// Compile a list of group entity types and bundles.
$target_types = [];
$target_bundles = [];
foreach (Og::groupTypeManager()->getGroupMap() as $entity_type => $bundles) {
$target_types[$entity_type] = \Drupal::entityTypeManager()->getDefinition($entity_type)->getLabel();
$bundle_info = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type);
foreach ($bundles as $bundle) {
$target_bundles[$entity_type][$bundle] = $bundle_info[$bundle]['label'];
foreach ($this->groupTypeManager->getGroupMap() as $entity_type => $bundle_ids) {
$target_types[$entity_type] = $this->entityTypeManager->getDefinition($entity_type)->getLabel();
$bundle_info = $this->entityTypeBundleInfo->getBundleInfo($entity_type);
foreach ($bundle_ids as $bundle_id) {
$target_bundles[$entity_type][$bundle_id] = $bundle_info[$bundle_id]['label'];
}
}

$form['og']['og_group_content_bundle'] = [
'#type' => 'checkbox',
'#title' => new TranslatableMarkup('Group content'),
'#default_value' => $this->bundle ? Og::isGroupContent($this->entityTypeId, $this->bundle) : FALSE,
'#default_value' => !$bundle->isNew() && $this->groupTypeManager->isGroupContent($entity_type_id, $bundle->id()),
'#description' => empty($target_bundles) ? new TranslatableMarkup('There are no group bundles defined.') : '',
];

Expand All @@ -172,7 +167,7 @@ protected function addGroupContent(array &$form, FormStateInterface $form_state)
'#default_value' => $target_type_default,
'#description' => new TranslatableMarkup('The entity type that can be referenced through this field.'),
'#ajax' => [
'callback' => [$this, 'ajaxCallback'],
'callback' => [static::class, 'ajaxCallback'],
'wrapper' => 'og-settings-wrapper',
],
'#states' => [
Expand All @@ -198,7 +193,7 @@ protected function addGroupContent(array &$form, FormStateInterface $form_state)
],
],
];
$form['#validate'][] = [get_class($this), 'validateTargetBundleElement'];
$form['#validate'][] = [static::class, 'validateTargetBundleElement'];
}
else {
// Don't show the settings, as there might be multiple OG audience fields
Expand All @@ -210,7 +205,7 @@ protected function addGroupContent(array &$form, FormStateInterface $form_state)
/**
* Form validate handler.
*/
public static function validateTargetBundleElement(array &$form, FormStateInterface $form_state) {
public static function validateTargetBundleElement(array &$form, FormStateInterface $form_state): void {
// If no checkboxes were checked for 'og_target_bundles', store NULL ("all
// bundles are referenceable") rather than empty array ("no bundle is
// referenceable" - typically happens when all referenceable bundles have
Expand All @@ -220,4 +215,17 @@ public static function validateTargetBundleElement(array &$form, FormStateInterf
}
}

/**
* Retrieves the entity type bundle object from the given form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* A bundle entity form.
*
* @return \Drupal\Core\Entity\RevisionableEntityBundleInterface
* The bundle.
*/
protected static function getEntityBundle(FormStateInterface $form_state): RevisionableEntityBundleInterface {
return $form_state->getFormObject()->getEntity();
}

}
2 changes: 1 addition & 1 deletion og_ui/src/Controller/OgUiController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\og_ui\Controller;

Expand Down
2 changes: 1 addition & 1 deletion og_ui/src/Form/AdminSettingsForm.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Drupal\og_ui\Form;

Expand Down
Loading