View source
<?php
namespace Drupal\auto_entitylabel\Form;
use Drupal\auto_entitylabel\AutoEntityLabelManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AutoEntityLabelForm extends ConfigFormBase {
protected $configFactory;
protected $entitymanager;
protected $route_match;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_manager, RouteMatchInterface $route_match) {
parent::__construct($config_factory);
$this->entitymanager = $entity_manager;
$this->route_match = $route_match;
$route_options = $this->route_match
->getRouteObject()
->getOptions();
$array_keys = array_keys($route_options['parameters']);
$this->entity_type_parameter = array_shift($array_keys);
$entity_type = $this->route_match
->getParameter($this->entity_type_parameter);
$this->entity_type_id = $entity_type
->id();
$this->entity_type_provider = $entity_type
->getEntityType()
->getProvider();
}
protected function getEditableConfigNames() {
return [
'auto_entitylabel.settings',
];
}
public function getFormId() {
return 'auto_entitylabel_settings_form';
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity.manager'), $container
->get('current_route_match'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$key = $this->entity_type_parameter . '_' . $this->entity_type_id;
$config = $this
->config('auto_entitylabel.settings');
$options = [
AutoEntityLabelManager::DISABLED => $this
->t('Disabled'),
AutoEntityLabelManager::ENABLED => $this
->t('Automatically generate the label and hide the label field'),
AutoEntityLabelManager::OPTIONAL => $this
->t('Automatically generate the label if the label field is left empty'),
];
$form['auto_entitylabel'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Automatic label generation for @type', [
'@type' => $this->entity_type_id,
]),
'#weight' => 0,
];
$form['auto_entitylabel'][$key . '_status'] = [
'#type' => 'radios',
'#default_value' => $config
->get($key . '_status'),
'#options' => $options,
];
$form['auto_entitylabel'][$key . '_pattern'] = [
'#type' => 'textarea',
'#title' => $this
->t('Pattern for the label'),
'#description' => $this
->t('Leave blank for using the per default generated label. Otherwise this string will be used as label. Use the syntax [token] if you want to insert a replacement pattern.'),
'#default_value' => $config
->get($key . '_pattern'),
];
if ($config
->get($key . '_php') && !\Drupal::currentUser()
->hasPermission('use PHP for auto entity labels')) {
$form['auto_entitylabel'][$key . '_pattern']['#disabled'] = TRUE;
$form['auto_entitylabel'][$key . '_pattern']['#description'] = $this
->t('You are not allowed the configure the pattern for the label, because you do not have the %permission permission.', [
'%permission' => $this
->t('Use PHP for automatic entity label patterns'),
]);
}
$module_handler = \Drupal::moduleHandler();
if ($module_handler
->moduleExists('token')) {
$token_info = $module_handler
->invoke($this->entity_type_provider, 'token_info');
$token_types = isset($token_info['types']) ? array_keys($token_info['types']) : [];
$form['auto_entitylabel']['token_help'] = [
'#theme' => 'token_tree_link',
'#token_types' => $token_types,
'#dialog' => TRUE,
];
}
$form['auto_entitylabel'][$key . '_php'] = [
'#access' => \Drupal::currentUser()
->hasPermission('use PHP for auto entity labels'),
'#type' => 'checkbox',
'#title' => $this
->t('Evaluate PHP in pattern.'),
'#description' => $this
->t('Put PHP code above that returns your string, but make sure you surround code in <code><?php</code> and <code>?></code>. Note that <code>$entity</code> and <code>$language</code> are available and can be used by your code.'),
'#default_value' => $config
->get($key . '_php'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory
->getEditable('auto_entitylabel.settings');
foreach ($form_state
->getValues() as $key => $value) {
$config
->set($key, $value);
}
$config
->save();
parent::submitForm($form, $form_state);
}
}