You are here

public function AutoEntityLabelForm::buildForm in Automatic Entity Label 8

Same name and namespace in other branches
  1. 8.3 src/Form/AutoEntityLabelForm.php \Drupal\auto_entitylabel\Form\AutoEntityLabelForm::buildForm()
  2. 8.2 src/Form/AutoEntityLabelForm.php \Drupal\auto_entitylabel\Form\AutoEntityLabelForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/AutoEntityLabelForm.php, line 100

Class

AutoEntityLabelForm
Class AutoEntityLabelForm.

Namespace

Drupal\auto_entitylabel\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $key = $this->entity_type_parameter . '_' . $this->entity_type_id;
  $config = $this
    ->config('auto_entitylabel.settings');

  /*
   * @todo
   *  Find a generic way of determining if the label is rendered on the
   *  entity form. If not, don't show 'auto_entitylabel_optional' option.
   */
  $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'),
  ];

  // Don't allow editing of the pattern if PHP is used, but the users lacks
  // permission for PHP.
  // @codingStandardsIgnoreLine
  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'),
    ]);
  }

  // Display the list of available placeholders if token module is installed.
  // @codingStandardsIgnoreLine
  $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'] = [
    // @codingStandardsIgnoreLine
    '#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>&lt;?php</code> and <code>?&gt;</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);
}