You are here

public function AutoEntityLabelForm::buildForm in Automatic Entity Label 8.3

Same name and namespace in other branches
  1. 8 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 164

Class

AutoEntityLabelForm
Class AutoEntityLabelForm.

Namespace

Drupal\auto_entitylabel\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config($this
    ->getConfigName());

  /*
   * @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'),
    AutoEntityLabelManager::PREFILLED => $this
      ->t('Automatically prefill the label'),
  ];

  // Create an array for description of the options.
  $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.'),
    ],
  ];

  // Shared across most of the settings on this page.
  $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,
  ];

  // Display the list of available placeholders if token module is installed.
  if ($this->moduleHandler
    ->moduleExists('token')) {

    // Special treatment for Core's taxonomy_vocabulary and taxonomy_term.
    $token_type = strtr($this->entityTypeBundleOf, [
      'taxonomy_' => '',
    ]);
    $form['auto_entitylabel']['token_help'] = [
      // #states needs a container to work, put token replacement link inside
      '#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']['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['auto_entitylabel']['preserve_titles'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Preserve already created node titles.'),
    '#description' => $this
      ->t('Check this to preserve the titles of the nodes that were already created.'),
    '#default_value' => $config
      ->get('preserve_titles'),
    '#states' => [
      'visible' => [
        ':input[name="status"]' => [
          'value' => AutoEntityLabelManager::ENABLED,
        ],
      ],
    ],
  ];
  $form['#attached']['library'][] = 'auto_entitylabel/auto_entitylabel.admin';
  $form['auto_entitylabel']['save'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Re-save'),
    '#description' => $this
      ->t('Re-save all labels.'),
    '#default_value' => $config
      ->get('save'),
  ];
  $form['auto_entitylabel']['chunk'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Chunk size'),
    '#description' => $this
      ->t('Number of entities to be processed per batch operation.'),
    '#default_value' => 50,
    '#min' => 1,
  ];
  return parent::buildForm($form, $form_state);
}