You are here

public function ImageAPIOptimizePipelineEditForm::form in Image Optimize (or ImageAPI Optimize) 8.2

Same name and namespace in other branches
  1. 8.3 src/Form/ImageAPIOptimizePipelineEditForm.php \Drupal\imageapi_optimize\Form\ImageAPIOptimizePipelineEditForm::form()
  2. 4.x src/Form/ImageAPIOptimizePipelineEditForm.php \Drupal\imageapi_optimize\Form\ImageAPIOptimizePipelineEditForm::form()

Gets the actual form array to be built.

Overrides ImageAPIOptimizePipelineFormBase::form

See also

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

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

File

src/Form/ImageAPIOptimizePipelineEditForm.php, line 50

Class

ImageAPIOptimizePipelineEditForm
Controller for image optimize pipeline edit form.

Namespace

Drupal\imageapi_optimize\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $user_input = $form_state
    ->getUserInput();
  $form['#title'] = $this
    ->t('Edit pipeline %name', [
    '%name' => $this->entity
      ->label(),
  ]);
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'imageapi_optimize/admin';

  // Build the list of existing image processors for this image optimize pipeline.
  $form['processors'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Processor'),
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'image-processor-order-weight',
      ],
    ],
    '#attributes' => [
      'id' => 'image-pipeline-processors',
    ],
    '#empty' => t('There are currently no processors in this pipeline. Add one by selecting an option below.'),
    // Render processors below parent elements.
    '#weight' => 5,
  ];
  foreach ($this->entity
    ->getProcessors() as $processor) {
    $key = $processor
      ->getUuid();
    $form['processors'][$key]['#attributes']['class'][] = 'draggable';
    $form['processors'][$key]['#weight'] = isset($user_input['processors']) ? $user_input['processors'][$key]['weight'] : NULL;
    $form['processors'][$key]['processor'] = [
      '#tree' => FALSE,
      'data' => [
        'label' => [
          '#plain_text' => $processor
            ->label(),
        ],
      ],
    ];
    $summary = $processor
      ->getSummary();
    if (!empty($summary)) {
      $summary['#prefix'] = ' ';
      $form['processors'][$key]['processor']['data']['summary'] = $summary;
    }
    $form['processors'][$key]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $processor
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $processor
        ->getWeight(),
      '#attributes' => [
        'class' => [
          'image-processor-order-weight',
        ],
      ],
    ];
    $links = [];
    $is_configurable = $processor instanceof ConfigurableImageAPIOptimizeProcessorInterface;
    if ($is_configurable) {
      $links['edit'] = [
        'title' => $this
          ->t('Edit'),
        'url' => Url::fromRoute('imageapi_optimize.processor_edit_form', [
          'imageapi_optimize_pipeline' => $this->entity
            ->id(),
          'imageapi_optimize_processor' => $key,
        ]),
      ];
    }
    $links['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('imageapi_optimize.processor_delete', [
        'imageapi_optimize_pipeline' => $this->entity
          ->id(),
        'imageapi_optimize_processor' => $key,
      ]),
    ];
    $form['processors'][$key]['operations'] = [
      '#type' => 'operations',
      '#links' => $links,
    ];
  }

  // Build the new image processor addition form and add it to the processor list.
  $new_processor_options = [];
  $processors = $this->imageAPIOptimizeProcessorManager
    ->getDefinitions();
  uasort($processors, function ($a, $b) {
    return strcasecmp($a['id'], $b['id']);
  });
  foreach ($processors as $processor => $definition) {
    $new_processor_options[$processor] = $definition['label'];
  }
  $form['processors']['new'] = [
    '#tree' => FALSE,
    '#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
    '#attributes' => [
      'class' => [
        'draggable',
      ],
    ],
  ];
  $form['processors']['new']['processor'] = [
    'data' => [
      'new' => [
        '#type' => 'select',
        '#title' => $this
          ->t('Processor'),
        '#title_display' => 'invisible',
        '#options' => $new_processor_options,
        '#empty_option' => $this
          ->t('Select a new processor'),
      ],
      [
        'add' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Add'),
          '#validate' => [
            '::processorValidate',
          ],
          '#submit' => [
            '::submitForm',
            '::processorSave',
          ],
        ],
      ],
    ],
    '#prefix' => '<div class="image-pipeline-new">',
    '#suffix' => '</div>',
  ];
  $form['processors']['new']['weight'] = [
    '#type' => 'weight',
    '#title' => $this
      ->t('Weight for new processor'),
    '#title_display' => 'invisible',
    '#default_value' => count($this->entity
      ->getProcessors()) + 1,
    '#attributes' => [
      'class' => [
        'image-processor-order-weight',
      ],
    ],
  ];
  $form['processors']['new']['operations'] = [
    'data' => [],
  ];
  return parent::form($form, $form_state);
}