You are here

public function ColorForm::form in Color API 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/Form/ColorForm.php, line 93

Class

ColorForm
Form handler for the Color add and edit forms.

Namespace

Drupal\colorapi\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $color_entity = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $color_entity
      ->label(),
    '#description' => $this
      ->t("Label for the Color."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $color_entity
      ->id(),
    '#machine_name' => [
      'exists' => [
        $this,
        'checkMachineName',
      ],
    ],
    '#disabled' => !$color_entity
      ->isNew(),
  ];
  $form['color'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Color'),
    '#default_value' => $color_entity
      ->getHexadecimal(),
    '#required' => TRUE,
    '#description' => $this
      ->t('Enter the color in hexadecimal string format #XXXXXX where X is a hexadecimal character (0-9, a-f).'),
    '#element_validate' => [
      '::colorElementValidate',
    ],
  ];
  if ($this->currentUser
    ->hasPermission('administer colors') && !$this->moduleHandler
    ->moduleExists('jquery_colorpicker')) {
    $url = Url::fromUri('https://www.drupal.org/project/jquery_colorpicker');
    $form['color']['#description'] .= '<br />' . $this
      ->t('Install the <a href=":url">JQuery Colorpicker module</a> to enable a color popup for this field.', [
      ':url' => $url
        ->toString(),
    ]);
  }
  return $form;
}