sites_group_overrides-1.x-dev/src/Form/BaseFieldConfigForm.php
src/Form/BaseFieldConfigForm.php
<?php declare(strict_types = 1);
namespace Drupal\sites_group_overrides\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
/**
* Provides a Group relation field sync form.
*/
final class BaseFieldConfigForm extends FormBase implements ContainerInjectionInterface {
/**
* Constructs a new BaseFieldConfigForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityFieldManagerInterface $entityFieldManager
) { }
protected EntityInterface $entity;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('entity_field.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'sites_group_overrides_base_field_config';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
foreach ($this->getRouteMatch()->getParameters() as $entity_type => $id) {
$this->entity = $this->entityTypeManager->getStorage($entity_type)->load($id);
}
// Add the sync option to field configs.
$form['sites_group_overrides'] = [
'#title' => $this->t('Sites group overrides'),
'#type' => 'details',
'#tree' => TRUE,
'#weight' => 12,
'#open' => TRUE,
];
/** @var GroupTypeInterface[] $group_types */
$group_types = $this->entityTypeManager->getStorage('group_type')->loadMultiple();
foreach ($group_types as $group_type) {
foreach ($group_type->getInstalledPlugins() as $plugin) {
$definition = $plugin->getPluginDefinition();
if ($definition->get('entity_type_id') != $this->entityTypeManager->getDefinition($this->getEntity()->getEntityTypeId())->getBundleOf()) {
continue;
}
$bundle = $definition->get('entity_type_id');
if ($definition->get('entity_bundle')) {
$bundle = $definition->get('entity_bundle');
}
if ($bundle != $this->getEntity()->id()) {
continue;
}
$relationship_bundle = $this->entityTypeManager->getStorage('group_relationship_type')->getRelationshipTypeId($group_type->id(), $plugin->getPluginId());
// Add the sync option to field configs.
$form['sites_group_overrides'][$relationship_bundle] = [
'#title' => $this->t('Sites group type: %group_type', ['%group_type' => $group_type->label()]),
'#type' => 'details',
'#tree' => TRUE,
'#open' => TRUE,
];
// Add the sync option on the entity type form for every base field.
foreach ($this->entityFieldManager->getBaseFieldDefinitions($this->getEntityType()->id()) as $definition) {
$form['sites_group_overrides'][$relationship_bundle][$definition->getName()] = [
'#title' => $definition->getLabel(),
'#type' => 'details',
'#tree' => TRUE,
'#open' => $definition->getConfig($this->getEntity()->id())->getThirdPartySetting('sites_group_overrides', $relationship_bundle)['use_overrides'] ?? FALSE,
];
$form['sites_group_overrides'][$relationship_bundle][$definition->getName()]['use_overrides'] = [
'#title' => $this->t('Make this field overridable per sites group'),
'#type' => 'checkbox',
'#default_value' => $definition->getConfig($this->getEntity()->id())->getThirdPartySetting('sites_group_overrides', $relationship_bundle)['use_overrides'] ?? FALSE,
];
$form['sites_group_overrides'][$relationship_bundle][$definition->getName()]['target_field_name'] = [
'#title' => $this->t('Select field for override storage'),
'#type' => 'select',
'#options' => array_map(function($d) {return t('@l (@mn)', ['@l' => $d->getLabel(), '@mn' => $d->getName()]);}, $this->entityFieldManager->getFieldDefinitions('group_relationship', $relationship_bundle)),
'#default_value' => $definition->getConfig($this->getEntity()->id())->getThirdPartySetting('sites_group_overrides', $relationship_bundle)['target_field_name'] ?? FALSE,
'#states' => [
'visible' => [':input[name="sites_group_overrides[' . $relationship_bundle . '][' . $definition->getName() . '][use_overrides]"]' => ['checked' => TRUE]],
],
];
}
}
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save configuration'),
'#button_type' => 'primary',
];
return $form;
}
public function getEntity() {
return $this->entity;
}
/**
* {@inheritdoc}
*/
protected function getEntityType(): ?EntityTypeInterface {
// Check whether the entity is a bundle entity type.
foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) {
if ($bundle_type = $entity_type->getBundleEntityType()) {
if ($bundle_type == $this->getEntity()->getEntityTypeId()) {
return $entity_type;
}
}
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$sync = $form_state->getValue('sites_group_overrides');
foreach ($sync as $bundle => $base_fields) {
foreach ($this->entityFieldManager->getBaseFieldDefinitions($this->entityTypeManager->getDefinition($this->getEntity()->getEntityTypeId())->getBundleOf()) as $definition) {
$base_field_override = $definition->getConfig($this->getEntity()->id());
if ($base_field_override->isNew() && !$sync[$bundle][$definition->getName()]['use_overrides']) {
continue;
}
$base_field_override->setThirdPartySetting('sites_group_overrides', $bundle, $sync[$bundle][$definition->getName()]);
$base_field_override->save();
}
}
}
}
