You are here

public function ReferenceTypeForm::form in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/src/Form/ReferenceTypeForm.php \Drupal\bibcite_entity\Form\ReferenceTypeForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

modules/bibcite_entity/src/Form/ReferenceTypeForm.php, line 48

Class

ReferenceTypeForm
Reference type form.

Namespace

Drupal\bibcite_entity\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\bibcite_entity\Entity\ReferenceTypeInterface $reference_type */
  $reference_type = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $reference_type
      ->label(),
    '#description' => $this
      ->t('Label for the Reference type.'),
    '#required' => TRUE,
  ];
  $form['description'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Description'),
    '#maxlength' => 255,
    '#default_value' => $reference_type
      ->getDescription(),
    '#description' => $this
      ->t('Short description of Reference type.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $reference_type
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\bibcite_entity\\Entity\\ReferenceType::load',
    ],
    '#disabled' => !$reference_type
      ->isNew(),
  ];
  $form['citekey_pattern'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Citation key pattern'),
    '#description' => $this
      ->t('Pattern for citation key automatic generation if value is not set. If pattern is not set <a href=":settings">global pattern</a> will be used.', [
      ':settings' => Url::fromRoute('bibcite_entity.reference.settings')
        ->toString(),
    ]),
    '#maxlength' => 255,
    '#default_value' => $reference_type
      ->getCitekeyPattern(),
  ];
  $form['token_help'] = [
    '#theme' => 'token_tree_link',
    '#token_types' => [
      'bibcite_reference',
    ],
    '#global_types' => TRUE,
  ];
  $form['override'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Override default properties'),
    '#default_value' => $reference_type
      ->isRequiredOverride(),
  ];
  $form['overrides'] = [
    '#type' => 'container',
    '#states' => [
      'visible' => [
        [
          ':input[name="override"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ],
  ];
  $form['overrides']['fields'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Field name'),
      $this
        ->t('Label'),
      $this
        ->t('Hint'),
      $this
        ->t('Required'),
    ],
    '#tree' => TRUE,
  ];
  $excluded_fields = [
    'id',
    'uuid',
    'langcode',
    'created',
    'changed',
    'type',
    'author',
    'revision_id',
    'revision_created',
    'revision_user',
    'revision_log_message',
    'status',
    'revision_default',
    'path',
    'metatag',
  ];
  $fields_configuration = $reference_type
    ->getFields();
  $fields = \Drupal::service('entity_field.manager')
    ->getBaseFieldDefinitions('bibcite_reference', 'bibcite_reference');

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field */
  foreach ($fields as $field) {
    $field_name = $field
      ->getName();
    if (in_array($field_name, $excluded_fields)) {
      continue;
    }
    $field_configuration = !empty($fields_configuration[$field_name]) ? $fields_configuration[$field_name] : [];
    $form['overrides']['fields'][$field_name] = [
      'name' => [
        '#markup' => new FormattableMarkup('@label (@name)', [
          '@label' => $field
            ->getLabel(),
          '@name' => $field_name,
        ]),
      ],
      'label' => [
        '#type' => 'textfield',
        '#size' => 30,
        '#default_value' => isset($field_configuration['label']) ? $field_configuration['label'] : '',
      ],
      'hint' => [
        '#type' => 'textfield',
        '#size' => 30,
        '#default_value' => isset($field_configuration['hint']) ? $field_configuration['hint'] : $field
          ->getDescription(),
      ],
      'required' => [
        '#type' => 'checkbox',
        '#default_value' => isset($field_configuration['required']) ? $field_configuration['required'] : FALSE,
      ],
    ];
  }

  /* @see \Drupal\node\NodeTypeForm::form() */
  $form['additional_settings'] = [
    '#type' => 'vertical_tabs',
  ];
  if ($this->operation == 'add') {
    $form['#title'] = $this
      ->t('Add reference type');

    /**
     * Create a reference with a fake bundle using the type's UUID so that we can
     * get the default values for workflow settings.
     *
     * @todo Make it possible to get default values without an entity.
     *   https://www.drupal.org/node/2318187
     */
    $reference = $this->entityTypeManager
      ->getStorage('bibcite_reference')
      ->create([
      'type' => $this
        ->getEntity()
        ->uuid(),
    ]);
  }
  else {
    $form['#title'] = $this
      ->t('Edit %label reference type', [
      '%label' => $reference_type
        ->label(),
    ]);

    // Create a reference to get the current values for workflow settings fields.
    $reference = $this->entityTypeManager
      ->getStorage('bibcite_reference')
      ->create([
      'type' => $this
        ->getEntity()
        ->id(),
    ]);
  }
  $form['workflow'] = [
    '#type' => 'details',
    '#title' => t('Publishing options'),
    '#group' => 'additional_settings',
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('Published'),
    '#default_value' => $reference->status->value,
    '#group' => 'workflow',
  ];
  $form['revision'] = [
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => $reference_type
      ->shouldCreateNewRevision(),
    '#description' => t('Users with the <em>Administer Bibliography &amp; Citation</em> permission will be able to override these options.'),
    '#group' => 'workflow',
  ];
  $form['submission'] = [
    '#type' => 'details',
    '#title' => t('Submission form settings'),
    '#group' => 'additional_settings',
    '#open' => TRUE,
  ];
  $form['submission']['preview_mode'] = [
    '#type' => 'radios',
    '#title' => t('Preview before submitting'),
    '#default_value' => $reference_type
      ->getPreviewMode(),
    '#options' => [
      DRUPAL_DISABLED => t('Disabled'),
      DRUPAL_OPTIONAL => t('Optional'),
      DRUPAL_REQUIRED => t('Required'),
    ],
  ];
  return $this
    ->protectBundleIdElement($form);
}