View source
<?php
namespace Drupal\auto_entitylabel\Form;
use Drupal\auto_entitylabel\AutoEntityLabelManager;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
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;
private $moduleHandler;
private $user;
protected $entityType;
protected $entityBundle;
protected $entityTypeBundleOf;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_manager, RouteMatchInterface $route_match, ModuleHandlerInterface $moduleHandler, AccountInterface $user) {
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->entityType = array_shift($array_keys);
$entity_type = $this->route_match
->getParameter($this->entityType);
$this->entityBundle = $entity_type
->id();
$this->entityTypeBundleOf = $entity_type
->getEntityType()
->getBundleOf();
$this->moduleHandler = $moduleHandler;
$this->user = $user;
}
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'), $container
->get('module_handler'), $container
->get('current_user'));
}
protected function getConfigName() {
return 'auto_entitylabel.settings.' . $this->entityTypeBundleOf . '.' . $this->entityBundle;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config($this
->getConfigName());
$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'),
AutoEntityLabelManager::PREFILLED => $this
->t('Automatically prefill the label'),
];
$options_description = [
AutoEntityLabelManager::DISABLED => [
'#description' => $this
->t('Selecting this option will disable the auto labels for the entity.'),
],
AutoEntityLabelManager::ENABLED => [
'#description' => $this
->t('Selecting this option will hide the title field and will generate a new option based on the pattern provided below.'),
],
AutoEntityLabelManager::OPTIONAL => [
'#description' => $this
->t('Selecting this option will make the label field optional and will generate a label if the label field is left empty.'),
],
AutoEntityLabelManager::PREFILLED => [
'#description' => $this
->t('Selecting this option will prefills the label field with the generated pattern provided below. This option provides limited token support because it only prefills the label and it will not be able to replace all the tokens like current node based tokens for ex: [node:nid] because that token has not been generated yet.'),
],
];
$invisible_state = [
'invisible' => [
':input[name="status"]' => [
'value' => AutoEntityLabelManager::DISABLED,
],
],
];
$form['auto_entitylabel'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Automatic label generation for @type', [
'@type' => $this->entityBundle,
]),
'#weight' => 0,
];
$form['auto_entitylabel']['status'] = [
'#type' => 'radios',
'#default_value' => $config
->get('status') ?: 0,
'#options' => $options,
];
$form['auto_entitylabel']['status'] += $options_description;
$form['auto_entitylabel']['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('pattern') ?: '',
'#attributes' => [
'class' => [
'pattern-label',
],
],
'#states' => $invisible_state,
];
if ($config
->get('php') && !$this->user
->hasPermission('use PHP for auto entity labels')) {
$form['auto_entitylabel']['pattern']['#disabled'] = TRUE;
$form['auto_entitylabel']['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'),
]);
}
if ($this->moduleHandler
->moduleExists('token')) {
$token_type = strtr($this->entityTypeBundleOf, [
'taxonomy_' => '',
]);
$form['auto_entitylabel']['token_help'] = [
'#type' => 'container',
'#states' => $invisible_state,
'token_link' => [
'#theme' => 'token_tree_link',
'#token_types' => [
$token_type,
],
'#dialog' => TRUE,
],
];
}
else {
$form['auto_entitylabel']['pattern']['#description'] .= ' ' . $this
->t('To get a list of available tokens install <a href=":drupal-token" target="blank">Token</a> module.', [
':drupal-token' => 'https://www.drupal.org/project/token',
]);
}
$form['auto_entitylabel']['php'] = [
'#access' => $this->user
->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.See the help section for an example'),
'#default_value' => $config
->get('php'),
'#states' => $invisible_state,
];
$form['auto_entitylabel']['escape'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Remove special characters.'),
'#description' => $this
->t('Check this to remove all special characters.'),
'#default_value' => $config
->get('escape'),
'#states' => $invisible_state,
];
$form['#attached']['library'][] = 'auto_entitylabel/auto_entitylabel.admin';
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory
->getEditable($this
->getConfigName());
$form_state
->cleanValues();
foreach ([
'status',
'pattern',
'php',
'escape',
] as $key) {
$config
->set($key, $form_state
->getValue($key));
}
$storage = $this->entityManager
->getStorage($this->entityType);
$entity_type = $storage
->getEntityType();
$prefix = $entity_type
->getConfigPrefix();
$config
->set('dependencies', [
'config' => [
$prefix . '.' . $this->entityBundle,
],
]);
$config
->save();
parent::submitForm($form, $form_state);
}
}