You are here

public function InsertFileWidgetUtility::process in Insert 8

Form API callback: Processes a file field element.

Parameters

array $element:

FormStateInterface $form_state:

Return value

array Element to render.

See also

\Drupal\file\Plugin\Field\FieldWidget\FileWidget::process()

1 call to InsertFileWidgetUtility::process()
InsertImageWidgetUtility::process in src/Utility/InsertImageWidgetUtility.php
@inheritdoc
1 method overrides InsertFileWidgetUtility::process()
InsertImageWidgetUtility::process in src/Utility/InsertImageWidgetUtility.php
@inheritdoc

File

src/Utility/InsertFileWidgetUtility.php, line 161

Class

InsertFileWidgetUtility

Namespace

Drupal\insert\Utility

Code

public function process($element, FormStateInterface $form_state) {

  // Prevent displaying the Insert button when the image is empty ("Add a new

  //file" row).
  if (count($element['fids']['#value']) === 0) {
    return null;
  }
  $fieldDefinitions = $this
    ->getFieldDefinitions($element, $form_state);
  if ($fieldDefinitions === null) {
    \Drupal::messenger()
      ->addMessage($this
      ->t('Unable to retrieve field definitions.'), 'error');
    return null;
  }
  $item = $element['#value'];
  if (!isset($item['fids']) || count($item['fids']) === 0) {
    return null;
  }
  $file = File::load($item['fids'][0]);
  $defaultStyleName = !empty($element['#insert_default']) ? $element['#insert_default'] : null;
  $insertStyles = $this
    ->retrieveInsertStyles($element['#insert_styles'], $defaultStyleName);
  $fieldType = $fieldDefinitions[$element['#field_name']]
    ->getType();
  $styleOptions = [];
  $element['insert'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'insert',
        'form-item',
        'container-inline',
        'inline',
      ],
      'data-insert-type' => $fieldType,
      'data-uuid' => $file
        ->uuid(),
    ],
  ];
  $element['insert']['insert_templates'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'insert-templates',
      ],
    ],
  ];
  foreach ($insertStyles as $styleName => $style) {
    $vars = static::aggregateVariables($styleName, $element, $fieldType);
    $element['insert']['insert_templates'][$styleName] = [
      '#type' => 'hidden',
      '#value' => count($item['fids']) === 0 ? '' : $this
        ->render($styleName, $vars),
      '#id' => $element['#id'] . '-insert-template-' . str_replace('_', '-', $styleName),
      '#name' => $element['#name'] . '[insert_template][' . $styleName . ']',
      '#attributes' => [
        'class' => [
          'insert-template',
        ],
      ],
    ];
    $styleOptions[$styleName] = $this
      ->getStyleLabel($style);
  }
  $element['insert']['insert_filename'] = [
    '#type' => 'hidden',
    '#value' => $file
      ->getFilename(),
    '#id' => $element['#id'] . '-insert-filename',
    '#name' => $element['#name'] . '[insert_filename]',
    '#attributes' => [
      'class' => [
        'insert-filename',
      ],
    ],
  ];
  $element = $this
    ->attachJavaScript($element, $fieldType, $this
    ->getStyleClasses($insertStyles));
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  $element['insert']['button'] = [
    '#theme' => 'insert_button_widget',
    '#type' => 'markup',
    '#options' => $styleOptions,
    '#widget' => [
      'type' => $fieldType,
    ],
    '#weight' => 5,
    '#fid' => $item['fids'][0],
    '#nid' => $node instanceof Node ? $node
      ->id() : '',
    '#insert_absolute' => !!$element['#insert_absolute'],
  ];
  if ($defaultStyleName !== null) {
    $element['insert']['button']['#default_value'] = $defaultStyleName;
  }
  return $element;
}