View source
<?php
declare (strict_types=1);
namespace Drupal\og_ui;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\og\Og;
use Drupal\og\OgGroupAudienceHelperInterface;
class BundleFormAlter {
protected $definition;
protected $bundle;
protected $bundleLabel;
protected $entityTypeId;
protected $entity;
public function __construct(EntityInterface $entity) {
$this->entity = $entity;
}
public function formAlter(array &$form, FormStateInterface $form_state) {
$this
->prepare($form, $form_state);
$this
->addGroupType($form, $form_state);
$this
->addGroupContent($form, $form_state);
}
public function ajaxCallback(array $form, FormStateInterface $form_state) {
return $form['og']['og_target_bundles'];
}
protected function prepare(array &$form, FormStateInterface $form_state) {
$this->bundle = $this->entity
->id();
$this->bundleLabel = Unicode::lcfirst($this->entity
->label());
$this->definition = $this->entity
->getEntityType();
$this->entityTypeId = $this->definition
->getBundleOf();
$form['og'] = [
'#type' => 'details',
'#title' => new TranslatableMarkup('Organic groups'),
'#collapsible' => TRUE,
'#group' => 'additional_settings',
'#description' => new TranslatableMarkup('This bundle may serve as a group, may belong to a group, or may not participate in OG at all.'),
];
}
protected function addGroupType(array &$form, FormStateInterface $form_state) {
if ($this->entity
->isNew()) {
$description = new TranslatableMarkup('Every entity in this bundle is a group which can contain entities and can have members.');
}
else {
$description = new TranslatableMarkup('Every "%bundle" is a group which can contain entities and can have members.', [
'%bundle' => Unicode::lcfirst($this->bundleLabel),
]);
}
$form['og']['og_is_group'] = [
'#type' => 'checkbox',
'#title' => new TranslatableMarkup('Group'),
'#default_value' => Og::isGroup($this->entityTypeId, $this->bundle),
'#description' => $description,
];
}
protected function addGroupContent(array &$form, FormStateInterface $form_state) {
$field = FieldConfig::loadByName($this->entityTypeId, $this->bundle, OgGroupAudienceHelperInterface::DEFAULT_FIELD);
$handler_settings = $field ? $field
->getSetting('handler_settings') : [];
$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'];
}
}
$form['og']['og_group_content_bundle'] = [
'#type' => 'checkbox',
'#title' => new TranslatableMarkup('Group content'),
'#default_value' => $this->bundle ? Og::isGroupContent($this->entityTypeId, $this->bundle) : FALSE,
'#description' => empty($target_bundles) ? new TranslatableMarkup('There are no group bundles defined.') : '',
];
if ($target_types) {
reset($target_types);
$target_type_default = $field && !empty($field
->getSetting('target_type')) ? $field
->getSetting('target_type') : key($target_types);
$ajax_value = $form_state
->getValue('og_target_type');
$target_type_default = $ajax_value ? $ajax_value : $target_type_default;
$form['og']['og_target_type'] = [
'#type' => 'select',
'#title' => new TranslatableMarkup('Target type'),
'#options' => $target_types,
'#default_value' => $target_type_default,
'#description' => new TranslatableMarkup('The entity type that can be referenced through this field.'),
'#ajax' => [
'callback' => [
$this,
'ajaxCallback',
],
'wrapper' => 'og-settings-wrapper',
],
'#states' => [
'visible' => [
':input[name="og_group_content_bundle"]' => [
'checked' => TRUE,
],
],
],
];
$form['og']['og_target_bundles'] = [
'#prefix' => '<div id="og-settings-wrapper">',
'#suffix' => '</div>',
'#type' => 'select',
'#title' => new TranslatableMarkup('Target bundles'),
'#options' => $target_bundles[$target_type_default],
'#default_value' => !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : NULL,
'#multiple' => TRUE,
'#description' => new TranslatableMarkup('The bundles of the entity type that can be referenced. Optional, leave empty for all bundles.'),
'#states' => [
'visible' => [
':input[name="og_group_content_bundle"]' => [
'checked' => TRUE,
],
],
],
];
$form['#validate'][] = [
get_class($this),
'validateTargetBundleElement',
];
}
else {
$form['og']['og_group_content_bundle']['#disabled'] = TRUE;
}
}
public static function validateTargetBundleElement(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('og_target_bundles') === []) {
$form_state
->setValue('og_target_bundles', NULL);
}
}
}