You are here

public function ManagerForm::buildForm in Hashtags 8

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 FormInterface::buildForm

File

src/Form/ManagerForm.php, line 23

Class

ManagerForm

Namespace

Drupal\hashtags\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $hashtags_field_name = \Drupal::config('hashtags.settings')
    ->get('hashtags_taxonomy_terms_field_name');
  $entity_types = _hashtags_get_content_entity_types();

  // make system content entity types to go first in the list
  array_unshift($entity_types, 'comment');
  array_unshift($entity_types, 'taxonomy_term');
  array_unshift($entity_types, 'user');
  array_unshift($entity_types, 'node');
  $entity_types = array_unique($entity_types);
  $form = array();
  $form['entity_types'] = array(
    '#tree' => true,
  );
  $form['info'] = array(
    '#markup' => $this
      ->t('Activate hashtags for corresponding bundles. After activation the hashtags will be available for Body field by default. Also you can activate hashtags for another fields that have Text type.'),
  );
  foreach ($entity_types as $entity_type) {
    $entity_type_label = _hashtags_get_entity_type_label($entity_type);
    $table_name = 'table-' . $entity_type;
    $form[$table_name] = [
      '#prefix' => '<h2>' . $entity_type_label . '</h2>',
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Name'),
        $this
          ->t('Operations'),
      ],
    ];
    $bundles = \Drupal::service('entity.manager')
      ->getBundleInfo($entity_type);
    foreach ($bundles as $bundle => $bundle_info) {
      if (is_object($bundle_info['label'])) {
        $label = $bundle_info['label']
          ->render();
      }
      else {
        if (is_string($bundle_info['label'])) {
          $label = $bundle_info['label'];
        }
      }
      if (!empty($label)) {
        $form[$table_name][$bundle]['label'] = [
          '#markup' => $label,
        ];
      }
      if (!_hashtags_is_field_exists($entity_type, $bundle, $hashtags_field_name)) {
        $form[$table_name][$bundle]["{$entity_type}__{$bundle}__add"] = [
          '#type' => 'submit',
          '#entity_type' => $entity_type,
          '#bundle' => $bundle,
          '#field_name' => $hashtags_field_name,
          '#value' => 'Activate Hashtags',
          '#submit' => [
            '::addField',
          ],
          '#name' => "{$entity_type}__{$bundle}__add",
        ];
      }
      else {
        $form[$table_name][$bundle]["{$entity_type}__{$bundle}__add"] = [
          '#type' => 'submit',
          '#entity_type' => $entity_type,
          '#bundle' => $bundle,
          '#field_name' => $hashtags_field_name,
          '#value' => 'Remove Hashtags',
          '#submit' => [
            '::removeField',
          ],
          '#name' => "{$entity_type}__{$bundle}__remove",
        ];
      }
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
    '#tree' => FALSE,
  );
  return $form;
}