You are here

public function CropTypeForm::form in Crop API 8

Same name and namespace in other branches
  1. 8.2 src/Form/CropTypeForm.php \Drupal\crop\Form\CropTypeForm::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

src/Form/CropTypeForm.php, line 18

Class

CropTypeForm
Form controller for crop type forms.

Namespace

Drupal\crop\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $type = $this->entity;
  $form['#title'] = $this->operation == 'add' ? $this
    ->t('Add crop type') : $this
    ->t('Edit %label crop type', [
    '%label' => $type
      ->label(),
  ]);
  $form['label'] = [
    '#title' => $this
      ->t('Name'),
    '#type' => 'textfield',
    '#default_value' => $type->label,
    '#description' => $this
      ->t('The human-readable name of this crop type. This name must be unique.'),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $type
      ->id(),
    '#machine_name' => [
      'exists' => [
        '\\Drupal\\crop\\Entity\\CropType',
        'load',
      ],
      'source' => [
        'label',
      ],
    ],
    // A crop type's machine name cannot be changed.
    '#disabled' => !$type
      ->isNew(),
    '#description' => $this
      ->t('A unique machine-readable name for this crop type. It must only contain lowercase letters, numbers, and underscores.'),
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $type->description,
    '#description' => $this
      ->t('Describe this crop type.'),
  ];
  $form['aspect_ratio'] = [
    '#title' => $this
      ->t('Aspect Ratio'),
    '#type' => 'textfield',
    '#default_value' => $type->aspect_ratio,
    '#attributes' => [
      'placeholder' => 'W:H',
    ],
    '#description' => $this
      ->t('Set an aspect ratio <b>eg: 16:9</b> or leave this empty for arbitrary aspect ratio'),
  ];
  $form['soft_limit'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Soft limit'),
    '#description' => $this
      ->t('Define crop size soft limit. Warning will be displayed if crop smaller than that is selected.'),
  ];
  $form['soft_limit']['soft_limit_width'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Width'),
    '#default_value' => $type->soft_limit_width,
    '#description' => $this
      ->t('Limit for width.'),
    '#size' => 60,
    '#field_suffix' => 'px',
    '#min' => 0,
  ];
  $form['soft_limit']['soft_limit_height'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Height'),
    '#default_value' => $type->soft_limit_height,
    '#description' => $this
      ->t('Limit for height.'),
    '#size' => 60,
    '#field_suffix' => 'px',
    '#min' => 0,
  ];
  $form['hard_limit'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Hard limit'),
    '#description' => $this
      ->t('Define crop size hard limit. User is not allowed to make a smaller selection than defined here.'),
  ];
  $form['hard_limit']['hard_limit_width'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Width'),
    '#default_value' => $type->hard_limit_width,
    '#description' => $this
      ->t('Limit for width'),
    '#size' => 60,
    '#field_suffix' => 'px',
    '#min' => 0,
  ];
  $form['hard_limit']['hard_limit_height'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Height'),
    '#default_value' => $type->hard_limit_height,
    '#description' => $this
      ->t('Limit for height.'),
    '#size' => 60,
    '#field_suffix' => 'px',
    '#min' => 0,
  ];
  return $form;
}