You are here

function _insert_field_process in Insert 8.2

Form API callback: Processes a file field element.

Parameters

array $element:

\Drupal\Core\Form\FormStateInterface $form_state:

array $form:

Return value

array

See also

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

1 string reference to '_insert_field_process'
_insert_add_process in ./insert.module
Returns an array with all paths removed, except for those containing the key '#insert'.

File

./insert.module, line 134

Code

function _insert_field_process(array $element, FormStateInterface $form_state, array $form) {
  $originalElement = $element;
  $element['#insert']['id'] = $element['#id'];
  $insertType = $element['#insert']['type'];
  $element['insert'] = [
    '#type' => $element['#insert']['settings']['align'] ? 'fieldset' : 'container',
    '#attributes' => [
      'class' => [
        'insert',
        'form-item',
        'container-inline',
        'inline',
      ],
      'data-insert-type' => $insertType,
    ],
  ];
  $continue = \Drupal::moduleHandler()
    ->invokeAll('insert_process', [
    &$insertType,
    &$element,
  ]);
  if (in_array(FALSE, $continue)) {
    return $originalElement;
  }
  $settings = $element['#insert']['settings'];
  $defaultStyleName = !empty($settings['default']) ? $settings['default'] : NULL;
  $insertStyles = _insert_retrieve_styles($settings['styles'], $defaultStyleName, $insertType);
  $insertElements = [
    &$element,
  ];
  if (count($continue) > 0 && is_string($continue[0])) {
    $insertElements = [];
    foreach ($element[$continue[0]] as $key => $value) {
      if (is_int($key)) {
        $insertElements[] =& $element[$continue[0]][$key];
      }
    }
  }
  foreach ($insertElements as &$insertElement) {
    $insertElement['insert']['insert_templates'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'insert-templates',
        ],
      ],
    ];
    $styleOptions = [];
    foreach ($insertStyles as $styleName => $style) {
      $vars = [
        'attributes' => [],
        'field_name' => $element['#field_name'],
        'field_type' => $insertType,
        'id' => $element['#insert']['id'],
        'insert_settings' => $settings,
        'style_name' => $styleName,
      ];
      if (in_array(FALSE, \Drupal::moduleHandler()
        ->invokeAll('insert_variables', [
        $insertType,
        &$element,
        $styleName,
        &$vars,
      ]))) {
        continue;
      }
      $insertElement['insert']['insert_templates'][$styleName] = [
        '#type' => 'hidden',
        '#value' => _insert_render($styleName, $vars),
        '#id' => $vars['id'] . '-insert-template-' . str_replace('_', '-', $styleName),
        '#name' => $element['#name'] . '[insert_template][' . $styleName . ']',
        '#attributes' => [
          'class' => [
            'insert-template',
          ],
        ],
      ];
      $styleOptions[$styleName] = _insert_get_style_label($style);
    }
    $config = \Drupal::config('insert.config');
    $insertElement['#attached']['drupalSettings']['insert'] = [
      'fileDirectoryPath' => \Drupal::config('system.file')
        ->get('default_scheme'),
      // These CSS classes will be retained from being dumped by CKEditor when
      // applying CKEditor styles using CKEditor's style drop-down.
      'classes' => [
        $insertType => [
          'insertClass' => $config
            ->get('css_classes.' . $insertType) !== NULL ? join(' ', $config
            ->get('css_classes.' . $insertType)) : '',
          'styleClass' => join(' ', _insert_get_style_classes($insertStyles)),
        ],
      ],
      'widgets' => [
        $insertType => $settings,
      ],
    ];

    /** @var \Drupal\node\NodeForm $form */
    $form = $form_state
      ->getFormObject();
    $node = $form
      ->getEntity();
    $insertElement['insert']['button'] = [
      '#theme' => 'insert_button_widget',
      '#type' => 'markup',
      '#options' => $styleOptions,
      '#weight' => 5,
      '#nid' => $node instanceof Node ? $node
        ->id() : '',
      '#insert' => $element['#insert'],
    ];
    if ($defaultStyleName !== NULL) {
      $insertElement['insert']['button']['#default_value'] = $defaultStyleName;
    }
  }
  return $element;
}