ImageStyleEditForm::form

public ImageStyleEditForm::form(array $form, FormStateInterface $form_state)

Gets the actual form array to be built.

Overrides ImageStyleFormBase::form

See also

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

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

File

core/modules/image/src/Form/ImageStyleEditForm.php, line 50

Class

ImageStyleEditForm
Controller for image style edit form.

Namespace

Drupal\image\Form

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
public function form(array $form, FormStateInterface $form_state) {
  $user_input = $form_state->getUserInput();
  $form['#title'] = $this->t('Edit style %name', array('%name' => $this->entity->label()));
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'image/admin';
 
  // Show the thumbnail preview.
  $preview_arguments = array('#theme' => 'image_style_preview', '#style' => $this->entity);
  $form['preview'] = array(
    '#type' => 'item',
    '#title' => $this->t('Preview'),
    '#markup' => drupal_render($preview_arguments),
    // Render preview above parent elements.
    '#weight' => -5,
  );
 
  // Build the list of existing image effects for this image style.
  $form['effects'] = array(
    '#type' => 'table',
    '#header' => array(
      $this->t('Effect'),
      $this->t('Weight'),
      $this->t('Operations'),
    ),
    '#tabledrag' => array(
      array(
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'image-effect-order-weight',
      ),
    ),
    '#attributes' => array(
      'id' => 'image-style-effects',
    ),
    '#empty' => t('There are currently no effects in this style. Add one by selecting an option below.'),
    // Render effects below parent elements.
    '#weight' => 5,
  );
  foreach ($this->entity->getEffects() as $effect) {
    $key = $effect->getUuid();
    $form['effects'][$key]['#attributes']['class'][] = 'draggable';
    $form['effects'][$key]['#weight'] = isset($user_input['effects']) ? $user_input['effects'][$key]['weight'] : NULL;
    $form['effects'][$key]['effect'] = array(
      '#tree' => FALSE,
      'data' => array(
        'label' => array(
          '#plain_text' => $effect->label(),
        ),
      ),
    );
 
    $summary = $effect->getSummary();
 
    if (!empty($summary)) {
      $summary['#prefix'] = ' ';
      $form['effects'][$key]['effect']['data']['summary'] = $summary;
    }
 
    $form['effects'][$key]['weight'] = array(
      '#type' => 'weight',
      '#title' => $this->t('Weight for @title', array('@title' => $effect->label())),
      '#title_display' => 'invisible',
      '#default_value' => $effect->getWeight(),
      '#attributes' => array(
        'class' => array('image-effect-order-weight'),
      ),
    );
 
    $links = array();
    $is_configurable = $effect instanceof ConfigurableImageEffectInterface;
    if ($is_configurable) {
      $links['edit'] = array(
        'title' => $this->t('Edit'),
        'url' => Url::fromRoute('image.effect_edit_form', [
          'image_style' => $this->entity->id(),
          'image_effect' => $key,
        ]),
      );
    }
    $links['delete'] = array(
      'title' => $this->t('Delete'),
      'url' => Url::fromRoute('image.effect_delete', [
        'image_style' => $this->entity->id(),
        'image_effect' => $key,
      ]),
    );
    $form['effects'][$key]['operations'] = array(
      '#type' => 'operations',
      '#links' => $links,
    );
  }
 
  // Build the new image effect addition form and add it to the effect list.
  $new_effect_options = array();
  $effects = $this->imageEffectManager->getDefinitions();
  uasort($effects, function($a, $b) {
    return strcasecmp($a['id'], $b['id']);
  });
  foreach ($effects as $effect => $definition) {
    $new_effect_options[$effect] = $definition['label'];
  }
  $form['effects']['new'] = array(
    '#tree' => FALSE,
    '#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
    '#attributes' => array('class' => array('draggable')),
  );
  $form['effects']['new']['effect'] = array(
    'data' => array(
      'new' => array(
        '#type' => 'select',
        '#title' => $this->t('Effect'),
        '#title_display' => 'invisible',
        '#options' => $new_effect_options,
        '#empty_option' => $this->t('Select a new effect'),
      ),
      array(
        'add' => array(
          '#type' => 'submit',
          '#value' => $this->t('Add'),
          '#validate' => array('::effectValidate'),
          '#submit' => array('::submitForm', '::effectSave'),
        ),
      ),
    ),
    '#prefix' => '<div class="image-style-new">',
    '#suffix' => '</div>',
  );
 
  $form['effects']['new']['weight'] = array(
    '#type' => 'weight',
    '#title' => $this->t('Weight for new effect'),
    '#title_display' => 'invisible',
    '#default_value' => count($this->entity->getEffects()) + 1,
    '#attributes' => array('class' => array('image-effect-order-weight')),
  );
  $form['effects']['new']['operations'] = array(
    'data' => array(),
  );
 
  return parent::form($form, $form_state);
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.