FormModePermission.php in Form Mode Control 8.2
File
src/FormModePermission.php
View source
<?php
namespace Drupal\form_mode_control;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FormModePermission implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $entityManager;
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function __construct(EntityTypeManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
public function roleToFormMode() {
$permissions = [];
$all_form_modes = $this->entityManager
->getStorage('entity_form_display')
->loadMultiple();
$configuration = \Drupal::configFactory()
->getEditable('form_mode_control.settings');
$config_purgatory = $configuration
->getRawData();
foreach ($all_form_modes as $id_form_mode => $form_mode) {
$machine_name_form_mode = explode('.', $id_form_mode);
$entity_type = $machine_name_form_mode[0];
$bundle = $machine_name_form_mode[1];
$form_mode_id = $machine_name_form_mode[2];
$permissions_key = 'use The form mode ' . $form_mode_id . ' linked to ' . $entity_type . ' entity( ' . $bundle . ' )';
unset($config_purgatory[$permissions_key]);
if ($form_mode
->status() == FALSE || !$form_mode_id) {
$configuration
->clear($permissions_key);
continue;
}
$title = $this
->t('Use the form mode %label_form_mode linked to %entity_type_id ( %bundle )', [
'%label_form_mode' => $form_mode_id,
'%entity_type_id' => form_mode_control_get_entity_type_label($entity_type),
'%bundle' => form_mode_control_get_bundle_label($entity_type, $bundle),
]);
$permissions[$permissions_key] = [
'title' => $title,
];
$configuration
->set($permissions_key, $id_form_mode);
}
foreach ($config_purgatory as $key => $data) {
$configuration
->clear($key);
}
$configuration
->save(TRUE);
$permissions['access_all_form_modes'] = [
'title' => $this
->t('Access all form modes'),
'description' => $this
->t('To access to a form mode, you must add ?display=form_mode_searched,else a form mode default was launched by default.'),
];
return $permissions;
}
protected function clearDataPermissions($data) {
foreach ($data as $id => $permission) {
if (!EntityFormDisplay::load($id)) {
\Drupal::configFactory()
->getEditable('form_mode_control.settings')
->clear($id);
}
}
}
}