You are here

class UloginWidget in uLogin (advanced version) 8

Provides Ulogin widget.

Plugin annotation

@FormElement("ulogin_widget");

Hierarchy

Expanded class hierarchy of UloginWidget

3 #type uses of UloginWidget
Ulogin::build in src/Plugin/Block/Ulogin.php
Builds and returns the renderable array for this block plugin.
ulogin_form_alter in ./ulogin.module
Implements hook_form_alter().
UserIdentity::buildForm in src/Form/UserIdentity.php
Form constructor.

File

src/Element/UloginWidget.php, line 14

Namespace

Drupal\ulogin\Element
View source
class UloginWidget extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $enabled_providers = [
      'vkontakte',
      'odnoklassniki',
      'mailru',
      'facebook',
      'twitter',
      'google',
      'yandex',
      'livejournal',
      'openid',
    ];
    $enabled_providers = array_filter(\Drupal::config('ulogin.settings')
      ->get('providers_enabled') ?: array_combine($enabled_providers, $enabled_providers));
    $main_providers = [
      'vkontakte',
      'odnoklassniki',
      'mailru',
      'facebook',
    ];
    $main_providers = array_filter(\Drupal::config('ulogin.settings')
      ->get('providers_main') ?: array_combine($main_providers, $main_providers));
    $required_fields = [
      'first_name',
      'last_name',
      'email',
      'nickname',
      'bdate',
      'sex',
      'photo',
      'photo_big',
      'country',
      'city',
    ];
    $required_fields = array_filter(\Drupal::config('ulogin.settings')
      ->get('fields_required') ?: array_combine($required_fields, $required_fields));
    $optional_fields = array_filter(\Drupal::config('ulogin.settings')
      ->get('fields_optional') ?: array_combine([
      'phone',
    ], [
      'phone',
    ]));
    $class = get_class($this);
    return [
      '#input' => FALSE,
      '#pre_render' => [
        [
          $class,
          'preRenderUloginWidget',
        ],
      ],
      '#theme' => 'ulogin_widget',
      '#theme_wrappers' => [
        'form_element',
      ],
      '#ulogin_id' => 'ulogin',
      '#ulogin_widget_id' => \Drupal::config('ulogin.settings')
        ->get('widget_id') ?: '',
      '#title' => \Drupal::config('ulogin.settings')
        ->get('widget_title') ?: '',
      '#weight' => \Drupal::config('ulogin.settings')
        ->get('widget_weight') ?: -100,
      '#ulogin_display' => \Drupal::config('ulogin.settings')
        ->get('display') ?: 'panel',
      '#ulogin_fields_required' => implode(',', $required_fields),
      '#ulogin_fields_optional' => implode(',', $optional_fields),
      '#ulogin_providers' => implode(',', array_intersect_assoc($main_providers, $enabled_providers)),
      '#ulogin_hidden' => implode(',', array_diff_assoc($enabled_providers, $main_providers)),
      '#ulogin_destination' => \Drupal::config('ulogin.settings')
        ->get('destination') ?: '',
      '#ulogin_redirect' => \Drupal::config('ulogin.settings')
        ->get('redirect') ?: 0,
      '#ulogin_icons_path' => \Drupal::config('ulogin.settings')
        ->get('icons_path') ?: '',
      '#ulogin_icons' => [],
      '#attached' => [
        'library' => [
          'ulogin/async',
        ],
      ],
    ];
  }

  /**
   * 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 \Drupal\file\Element\ManagedFile::processManagedFile()
   * @see \Drupal\Core\Form\FormBuilderInterface::doBuildForm()
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
FormElement::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 15
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UloginWidget::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
UloginWidget::preRenderUloginWidget public static function Render API callback: Hides display of the upload or remove controls.