csna.module 5.46 KB
<?php

use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;

/**
 * Returns an array of OAuth Third Party authentication providers.
 *
 * CSNA's API Framework provides a method for any enabled module to define
 * additional authentication providers. This function gathers information on
 * provider authentication methods defined by enabled modules.
 *
 * @param bool $valid
 * @return array An associative array of providers keyed by the provider_id.
 * An associative array of providers keyed by the provider_id.
 * @see hook_csna_provider_info()
 */
function csna_providers($valid = FALSE) {
    static $providers;

    if (!isset($providers)) {
        $providers = array();

        // Build the providers array, including module names for the purpose
        // of including files if necessary.
        // An associative array of providers objects keyed by the provider_id.
        $module_handler = \Drupal::moduleHandler();

        foreach ($module_handler->getImplementations('csna_provider_info') as $module) {
            foreach ($module_handler->invoke($module, 'csna_provider_info') as $provider_id => $provider) {
                $config = \Drupal::config('csna.' . $provider);
                $provider['provider_id'] = $provider_id;
                $provider['module'] = $module;
                $provider['key'] = $config->get('csna_' . $provider_id . '_key');
                $provider['secret'] = $config->get('csna_' . $provider_id . '_secret');
                $providers[$provider_id] = $provider;
                if ($valid && (empty($provider['key']) || empty($provider['secret']))) {
                    unset($providers[$provider_id]);
                }
            }
        }

        $module_handler->alter('csna_provider_info', $providers);

        foreach ($providers as $provider_id => & $provider) {
            $defaults = array(
                'provider_id' => $provider_id,
                'base' => $provider_id,
                'title' => '',
                'description' => '',
                'active' => FALSE,
                'callback' => Url::fromRoute('csna.callback',array('csna_provider' => $provider_id), array('absolute' => TRUE)),
                'file' => '',
            );

            $provider += $defaults;

            // Default the display title to the title if necessary.  The display
            // title is used in instances where the login method has an official
            // name but a different method of display on selection forms (like some
            // text and a set of images).
            if (empty($provider['display_title'])) {
                $provider['display_title'] = $provider['title'];
            }

            // Default the short title to the title if necessary.  Like the display
            // title, the short title is an alternate way of displaying the title to
            // the user consisting of plain text but with unnecessary information
            // stripped off.
            if (empty($provider[$provider_id]['short_title'])) {
                $provider['short_title'] = $provider['title'];
            }
        }
    }

    return $providers;
}


function csna_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    //$form_id == 'user_login' || $form_id == 'user_login_block' || $form_id == 'user_register_form'
    if ( $form_id == 'user_login_form' ) {
        $providers = csna_providers();

        // if admin form
        if (empty($form['administer_users']['#value'])) {
            $csna_provider = array(
                '#theme' => 'csna_providers',
                '#attributes' => array('providers' => $providers),
            );

            if (isset($form['submitted']['user_login_block']['#suffix']) && !empty($form['submitted']['user_login_block']['#suffix'])) {
                $form['submitted']['user_login_block']['#suffix'] .= drupal_render($csna_provider);
            }
            else {
                $form['submitted']['user_login_block']['#suffix'] = drupal_render($csna_provider);
            }
        }
    }
    dpm($form_id);
   // dpm($form);
}
/**
 * Implements hook_theme().
 */
function csna_theme($existing, $type, $theme, $path) {
    $providers = csna_providers();
    return array(
        'csna_providers' => array(
            'variables' => array('providers' => $providers),
        ),
    );
}



/**
 * Render a themed list of links to providers authorized login URL pages.
 *
 * @param array $providers
 *   An associative array of providers keyed by the provider_id, as returned by
 *   the function csna_providers().
 *   Mostly the display_title property of providers is used in this function
 *   to display a custom title, name or even HTML to be linked to corresponding
 *   provider's authorized login URL page. See API documentation for more
 *   information on providers available properties.
 *
 * @see csna_providers()
 * @see hook_csna_provider_info()
 */
function template_preprocess_csna_providers(&$vars) {
    dpm($vars);
    $providers = $vars['providers'];
    $items = array();
    foreach ($providers as $provider_id => $provider) {
        $items[] = array(
            'data' => \Drupal::l($provider['display_title'], Url::fromRoute('csna.redirect', array('csna_provider' => $provider_id), array('html' => TRUE))),
            'id' => 'csna_provider_' . $provider_id,
        );
    }

    $item_list = array(
        '#type' => 'item_list',
        'items' => $items,
        'attributes' => array('class' => 'csna-items'),
    );
    return drupal_render($item_list);
}