sites_group_overrides-1.x-dev/src/FormDecorator/TargetConfigFields.php
src/FormDecorator/TargetConfigFields.php
<?php
declare(strict_types=1);
namespace Drupal\sites_group_overrides\FormDecorator;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\form_decorator\FormDecoratorBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Disable field config forms for snyced field configs.
*
* @FormDecorator(
* hook = "form_field_config_edit_form_alter"
* )
*/
final class TargetConfigFields extends FormDecoratorBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $configuration, $plugin_id, $plugin_definition) {
return new self($configuration, $plugin_id, $plugin_definition,
$container->get('entity_type.manager'),
$container->get('entity_field.manager')
);
}
/**
* {@inheritdoc}
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected EntityTypeManagerInterface $entityTypeManager,
protected EntityFieldManagerInterface $entityFieldManager
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ...$args) {
$form = parent::buildForm($form, $form_state, ...$args);
if ($this->getEntity()->get('entity_type') != 'group_relationship') {
return $form;
}
$relationship_bundle = $this->entityTypeManager->getStorage('group_relationship_type')->load($this->getEntity()->get('bundle'));
$plugin = $relationship_bundle->getPlugin();
$definition = $plugin->getPluginDefinition();
foreach ($this->entityFieldManager->getFieldDefinitions($definition->get('entity_type_id'), $definition->get('entity_bundle')) as $field_definition) {
if (!$field_definition instanceof FieldConfigInterface) {
continue;
}
$settings = $field_definition->getThirdPartySetting('sites_group_overrides', $this->getEntity()->get('bundle'));
if (empty($settings) || $settings['target_field_name'] != $this->getEntity()->get('field_name') || $settings['use_existing_field']) {
continue;
}
foreach ($form as $key => &$element) {
if (substr($key, 0, 1) == '#') {
continue;
}
$element['#disabled'] = TRUE;
}
$form['message'] = [
'#type' => 'markup',
'#markup' => $this->t('The field config is synced with <a href="@url">@field</a>. <br><br>', [
'@field' => $field_definition->getName(),
'@url' => Url::fromRoute('entity.field_config.' . $definition->get('entity_type_id') . '_field_edit_form', [
$this->entityTypeManager->getDefinition($definition->get('entity_type_id'))->getBundleEntityType() => $definition->get('entity_bundle'),
'field_config' => $field_definition->getConfig($definition->get('entity_bundle'))->id(),
])->toString(),
]),
'#weight' => -120,
];
}
return $form;
}
}
