View source
<?php
namespace Drupal\form_mode_control\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class FormModeConfigForm extends ConfigFormBase {
public function getFormId() {
return 'form_mode_config';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$entity_manager = \Drupal::getContainer()
->get('entity_type.bundle.info');
$form_state
->setRebuild();
$entities_has_form_mode = $this
->entitiesHasFormMode();
$roles = Role::loadMultiple();
$configuration = \Drupal::configFactory()
->getEditable('form_mode_control.settings');
$data = $configuration
->getRawData();
$entities_actives = array_intersect($this
->getEntitiesFormModeActivated(), array_keys($entities_has_form_mode));
$form['information'] = [
'#type' => 'vertical_tabs',
];
foreach ($entities_actives as $machine_name_entity) {
$bundles_has_form_modes_activated = array_values(array_unique($this
->filterBundles()));
$bundles = array_keys($this
->getBundleFormEntityType($machine_name_entity));
$bundle_finished = array_values(array_intersect($bundles_has_form_modes_activated, $bundles));
$form['details_entity_type_' . $machine_name_entity] = [
'#type' => 'vertical_tabs',
'#title' => form_mode_control_get_entity_type_label($machine_name_entity),
'#open' => TRUE,
'#group' => 'information',
];
foreach ($bundle_finished as $id_bundle) {
$form['details_entity_type_' . $machine_name_entity]['details_bundle_' . $machine_name_entity . '_' . $id_bundle] = [
'#type' => 'details',
'#title' => $entity_manager
->getBundleInfo($machine_name_entity)[$id_bundle]['label'] . ' (' . form_mode_control_get_entity_type_label($machine_name_entity) . ') ',
'#open' => TRUE,
'#group' => 'information',
];
$form['details_entity_type_' . $machine_name_entity]['details_bundle_' . $machine_name_entity . '_' . $id_bundle]['details_form_mode_creation' . '_' . $machine_name_entity . '_' . $id_bundle] = [
'#type' => 'details',
'#title' => $this
->t('Creation @bundle', [
'@bundle' => form_mode_control_get_bundle_label($machine_name_entity, $id_bundle),
]),
'#open' => TRUE,
];
$form['details_entity_type_' . $machine_name_entity]['details_bundle_' . $machine_name_entity . '_' . $id_bundle]['details_form_mode_modification' . '_' . $machine_name_entity . '_' . $id_bundle] = [
'#type' => 'details',
'#title' => $this
->t('Modification @bundle', [
'@bundle' => form_mode_control_get_bundle_label($machine_name_entity, $id_bundle),
]),
'#open' => TRUE,
];
foreach ($roles as $id_role => $role) {
$options = $this
->filterFormModeByEntityType($machine_name_entity, $id_bundle, $role);
if (count($options) > 0) {
$id_creation = isset($data['creation+' . $id_role . '+' . $machine_name_entity . '+' . $id_bundle]) ? $data['creation+' . $id_role . '+' . $machine_name_entity . '+' . $id_bundle] : $machine_name_entity . '.' . $id_bundle . '.default';
$form['details_entity_type_' . $machine_name_entity]['details_bundle_' . $machine_name_entity . '_' . $id_bundle]['details_form_mode_creation' . '_' . $machine_name_entity . '_' . $id_bundle]['creation+' . $id_role . '+' . $machine_name_entity . '+' . $id_bundle] = [
'#type' => 'select',
"#options" => $options,
'#title' => $role
->label(),
'#default_value' => $id_creation,
'#group' => 'information',
];
$id_modification = isset($data['modification+' . $id_role . '+' . $machine_name_entity . '+' . $id_bundle]) ? $data['modification+' . $id_role . '+' . $machine_name_entity . '+' . $id_bundle] : $machine_name_entity . '.' . $id_bundle . '.default';
$form['details_entity_type_' . $machine_name_entity]['details_bundle_' . $machine_name_entity . '_' . $id_bundle]['details_form_mode_modification' . '_' . $machine_name_entity . '_' . $id_bundle]['modification+' . $id_role . '+' . $machine_name_entity . '+' . $id_bundle] = [
'#type' => 'select',
"#options" => $options,
'#title' => $role
->label(),
'#default_value' => $id_modification,
'#group' => 'information',
];
}
}
}
}
$form = parent::buildForm($form, $form_state);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$all_values = $form_state
->getValues();
unset($all_values['submit']);
unset($all_values['form_build_id']);
unset($all_values['form_token']);
unset($all_values['submit']);
unset($all_values['form_id']);
unset($all_values['op']);
$configuration = \Drupal::configFactory()
->getEditable('form_mode_control.settings');
$cleared_data = [];
foreach ($configuration
->getRawData() as $data_key => $data_value) {
if (substr_count($data_key, "modification_") != 0 || substr_count($data_key, "creation_") != 0 || substr_count($data_key, "modification+") != 0 || substr_count($data_key, "creation+") != 0) {
$configuration
->clear($data_key);
$cleared_data[$data_key] = $data_key;
}
}
foreach ($all_values as $form_state_key => $form_mode_id_associated) {
$display = $form_state
->getValue($form_state_key);
if (substr_count($form_state_key, "modification+") != 0 || substr_count($form_state_key, "creation+") != 0) {
$configuration
->set($form_state_key, $display);
}
}
$configuration
->save();
parent::submitForm($form, $form_state);
}
protected function getEditableConfigNames() {
return [
'form_mode_control.settings',
];
}
protected function entitiesHasFormMode() {
$entitiesHasFormMode = [];
$all_entities = \Drupal::entityTypeManager()
->getDefinitions();
foreach ($all_entities as $entity_type_id => $entity_type) {
if ($entity_type
->get('field_ui_base_route') && $entity_type
->hasFormClasses()) {
$entitiesHasFormMode[$entity_type_id] = $entity_type
->getLabel();
}
}
return $entitiesHasFormMode;
}
protected function importPermissionByFormModId($form_modes_id) {
$permissions = [];
$data = \Drupal::configFactory()
->getEditable('form_mode_control.settings')
->getRawData();
foreach ($data as $key => $value) {
if (substr_count($key, "linked to") != 0) {
$permissions[$value] = $key;
}
}
return isset($permissions[$form_modes_id]) ? $permissions[$form_modes_id] : NULL;
}
protected function filterFormModeByEntityType($entity_type_id, $bundle_id, RoleInterface $role) {
$storage = \Drupal::entityTypeManager()
->getStorage('entity_form_display');
$form_modes_ids = $storage
->loadMultiple();
$id_form_mode_searched = [];
$authenticated_role = Role::load(RoleInterface::AUTHENTICATED_ID);
foreach ($form_modes_ids as $form_modes_id => $form_mode_configuration) {
$aux = explode(".", $form_modes_id);
$entity_type_to_filter = $aux[0];
$bundle_id_to_filter = $aux[1];
$display = $aux[2];
if ($entity_type_to_filter == $entity_type_id && $bundle_id_to_filter == $bundle_id) {
$has_permission = $role
->hasPermission('access_all_form_modes') || $role
->id() !== RoleInterface::ANONYMOUS_ID && $authenticated_role
->hasPermission('access_all_form_modes');
if (!$has_permission) {
$imported_permission = $this
->importPermissionByFormModId($form_modes_id);
$has_permission = $role
->hasPermission($imported_permission) || $role
->id() !== RoleInterface::ANONYMOUS_ID && $authenticated_role
->hasPermission($imported_permission);
}
if ($has_permission && $form_mode_configuration
->status()) {
$id_form_mode_searched[$form_modes_id] = form_mode_control_get_label_from_machine_name($entity_type_to_filter, $bundle_id_to_filter, $display);
}
}
}
return $id_form_mode_searched;
}
protected function getEntitiesFormModeActivated() {
$all_form_modes = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->loadMultiple();
$entities = [];
foreach ($all_form_modes as $id_form_mode => $form_mode) {
$machine_name_form_mode = explode('.', $id_form_mode);
$form_mode_id = $machine_name_form_mode[2];
$entity_type = $machine_name_form_mode[0];
if ($form_mode
->status() && $form_mode_id != "default") {
$entities[$id_form_mode] = $entity_type;
}
}
return $entities;
}
public function filterBundles() {
$bundles_final = [];
$form_modes_activated = array_keys($this
->getEntitiesFormModeActivated());
foreach ($form_modes_activated as $form_mode_id => $bundle) {
$bundles_final[] = explode('.', $bundle)[1];
}
return $bundles_final;
}
protected function getBundleFormEntityType($entity_type_id) {
$entity_manager = \Drupal::getContainer()
->get('entity_type.bundle.info');
return $entity_manager
->getAllBundleInfo()[$entity_type_id];
}
}