You are here

public static function UloginWidget::preRenderUloginWidget in uLogin (advanced version) 8

Render API callback: Hides display of the upload or remove controls.

Upload controls are hidden when a file is already uploaded. Remove controls are hidden when there is no file attached. Controls are hidden here instead of in \Drupal\file\Element\ManagedFile::processManagedFile(), because #access for these buttons depends on the managed_file element's #value. See the documentation of \Drupal\Core\Form\FormBuilderInterface::doBuildForm() for more detailed information about the relationship between #process, #value, and #access.

Because #access is set here, it affects display only and does not prevent JavaScript or other untrusted code from submitting the form as though access were enabled. The form processing functions for these elements should not assume that the buttons can't be "clicked" just because they are not displayed.

See also

\Drupal\file\Element\ManagedFile::processManagedFile()

\Drupal\Core\Form\FormBuilderInterface::doBuildForm()

File

src/Element/UloginWidget.php, line 108

Class

UloginWidget
Provides Ulogin widget.

Namespace

Drupal\ulogin\Element

Code

public static function preRenderUloginWidget($element) {

  // If we already have a file, we don't want to show the upload controls.
  $element['#ulogin_id'] = Html::getId($element['#ulogin_id']);
  $element['#attached']['drupalSettings']['ulogin'][] = $element['#ulogin_id'];
  if ($element['#ulogin_widget_id']) {
    $element['#theme'] = 'ulogin_widget_id';
    return $element;
  }
  if ($element['#ulogin_redirect']) {
    $callback = 'Drupalulogintoken';
    $redirect = '';
    $element['#attached']['library'][] = 'ulogin/ulogin';
  }
  else {
    $callback = '';
    $redirect = UloginHelper::tokenUrl($element['#ulogin_destination']);
  }
  $element['#ulogin_data'] = 'display=' . $element['#ulogin_display'] . ';fields=' . $element['#ulogin_fields_required'] . ';optional=' . $element['#ulogin_fields_optional'] . ';callback=' . $callback . ';redirect_uri=' . $redirect;
  if ($element['#ulogin_display'] == 'window') {
    $element['#theme'] = 'ulogin_widget_window';
    return $element;
  }
  if ($element['#ulogin_display'] == 'buttons') {
    $element['#theme'] = 'ulogin_widget_buttons';
    $icons = [];
    if (!empty($element['#ulogin_icons_path'])) {
      foreach (file_scan_directory($element['#ulogin_icons_path'], '//') as $icon) {
        $icons[$icon->name] = $icon->uri;
      }
    }
    if (empty($icons)) {
      $icons = $element['#ulogin_icons'];
    }
    $element['icons'] = [];
    foreach ($icons as $key => $value) {
      $image_info = \Drupal::service('image.factory')
        ->get($value);
      $element['icons'][] = [
        '#theme' => 'image',
        '#uri' => $value,
        '#alt' => $key,
        '#title' => $key,
        '#width' => $image_info
          ->getWidth(),
        '#height' => $image_info
          ->getHeight(),
        '#attributes' => [
          'data-uloginbutton' => $key,
          'class' => 'ulogin-icon-' . $key,
        ],
      ];
    }
    return $element;
  }
  $element['#ulogin_data'] .= ';providers=' . $element['#ulogin_providers'] . ';hidden=' . $element['#ulogin_hidden'];
  return $element;
}