You are here

class DrupalGapController in DrupalGap 8.2

Same name and namespace in other branches
  1. 8 src/Controller/DrupalGapController.php \Drupal\DrupalGap\Controller\DrupalGapController
  2. 8.0.x src/Controller/DrupalGapController.php \Drupal\DrupalGap\Controller\DrupalGapController

Returns responses for DrupalGap module routes.

Hierarchy

Expanded class hierarchy of DrupalGapController

File

src/Controller/DrupalGapController.php, line 17
Contains \Drupal\drupalgap\Controller\DrupalGapController.

Namespace

Drupal\drupalgap\Controller
View source
class DrupalGapController extends ControllerBase {

  /**
   * Return the DrupalGap configuration page.
   *
   * @return string
   *   A render array containing our DrupalGap configuration page content.
   */
  public function drupalgapConfig() {
    $output = array();
    $output['drupalgap'] = array(
      '#attached' => array(
        'library' => array(
          'drupalgap/drupalgap.config',
        ),
      ),
      // Render the Status connection and some help docs.
      '#markup' => \Drupal::service('renderer')
        ->render($this
        ->drupalStatus()),
    );
    return $output;
  }

  /**
   * Checks the current status and add help link.
   *
   * @return array
   *   An array to be rendered with the id for the div where we will check the status and an information link.
   */
  public function drupalStatus() {

    // Set div id for system connect status message box.
    $div_id = 'drupalgap-system-connect-status-message';

    // Build more info text and help link string.
    $msg = t('Please refer to the <a href="@help_page">DrupalGap Module Help Page</a> for more information.', array(
      '@help_page' => 'admin/help/drupalgap',
    ));

    // Create output fieldsets.
    $output = array(
      'drupalgap_system_connect_status' => array(
        '#theme' => 'fieldset',
        '#title' => t('System Connect Status'),
        '#description' => '<div id="' . $div_id . '">&nbsp;</div>',
      ),
      'drupalgap_information' => array(
        '#theme' => 'fieldset',
        '#title' => t('More Information'),
        '#description' => "<p>{$msg}</p>",
      ),
    );
    return $output;
  }

  /**
   * @see https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityManagerInterface.php/interface/EntityManagerInterface/8
   */
  public function drupalgapConnect() {
    $response = new Response();
    $result = new \stdClass();

    //$entity_type = 'node';

    //$bundle = 'course';

    //$field_name = 'field_address';

    // @TODO make this configurable.
    $ok_entity_types = array(
      'comment',
      'file',
      'node',
      'taxonomy_term',
      'user',
    );

    // Field map.
    $result->fieldMap = array();
    $fieldMap = \Drupal::entityManager()
      ->getFieldMap();
    foreach ($fieldMap as $entity_type => $_fieldMap) {
      if (!in_array($entity_type, $ok_entity_types)) {
        continue;
      }
      $result->fieldMap[$entity_type] = $_fieldMap;
    }

    // All bundle info.
    $allBundleInfo = \Drupal::entityManager()
      ->getAllBundleInfo();
    $result->allBundleInfo = array();
    foreach ($allBundleInfo as $entity_type => $_allBundleInfo) {
      if (!in_array($entity_type, $ok_entity_types)) {
        continue;
      }
      $result->allBundleInfo[$entity_type] = $_allBundleInfo;
    }

    //$result->getBundleInfo = \Drupal::entityManager()->getBundleInfo($entity_type);

    // Field definitions and storage configs.
    $result->fieldDefinitions = array();
    $result->fieldStorageConfig = array();
    $result->entityFormDisplay = array();

    //$result->FieldConfig = \Drupal\field\Entity\FieldConfig::loadByName($entity_type, $bundle, $field_name);

    //$result->fieldDefinitions = \Drupal::entityManager()->getFieldDefinitions($entity_type, $bundle);

    // For each entity type...
    foreach ($ok_entity_types as $entity_type) {

      // Add the field definition for each bundle...
      $result->fieldDefinitions[$entity_type] = array();
      $result->entityFormDisplay[$entity_type] = array();
      foreach ($result->allBundleInfo[$entity_type] as $bundle_name => $bundle) {
        $result->fieldDefinitions[$entity_type][$bundle_name] = \Drupal::entityManager()
          ->getFieldDefinitions($entity_type, $bundle_name);
        $form_mode = 'default';
        $result->entityFormDisplay[$entity_type][$bundle_name] = entity_get_form_display($entity_type, $bundle_name, $form_mode)
          ->getMode();
      }

      // Add the field storage config for each field on the entity type.
      $result->fieldStorageConfig[$entity_type] = array();
      foreach ($result->fieldMap[$entity_type] as $field_name => $_data) {

        // @todo we should be using the loadByName function here, but it isn't
        // working
        // @see http://drupal.stackexchange.com/q/167001/10645

        //$result->fieldStorageConfig[$entity_type][$field_name] =

        //\Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type, $field_name);
        $result->fieldStorageConfig[$entity_type][$field_name] = \Drupal::config('field.storage.' . $entity_type . '.' . $field_name)
          ->get();
      }

      // DISPLAYS

      //core.entity_form_display.node.course.default

      //core.entity_view_display.node.course.default

      //core.entity_view_mode.node.full
    }

    // May be useful at some point...

    //$result->getAllViewModes = \Drupal::entityManager()->getAllViewModes();

    //$result->getViewModes = \Drupal::entityManager()->getViewModes($entity_type);

    //$result->getAllFormModes = \Drupal::entityManager()->getAllFormModes();

    // Doesn't work.

    //\Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type, $field_name);

    // Does work.

    //\Drupal::config('field.storage.node.field_address')->get();

    //$result->getFieldStorageDefinitions = \Drupal::entityManager()->getFieldStorageDefinitions($entity_type);

    //$result->loadMultiple = \Drupal::entityManager()->getStorage('field_storage_config')->loadMultiple();
    $response
      ->setContent(json_encode($result));
    $response->headers
      ->set('Content-Type', 'application/json');
    return $response;
  }

  /**
   *
   *
   */
  public function drupalgapSystemConnect() {
    $response = new Response();

    // @see https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Session!AccountProxyInterface.php/interface/AccountProxyInterface/8
    $user = \Drupal::currentUser();
    $_account = $user
      ->getAccount();
    $account = new \stdClass();
    $account->uid = $_account
      ->id();
    $account->name = $_account
      ->getUsername();
    $account->roles = $_account
      ->getRoles();
    foreach ($_account as $key => $value) {
      $account->{$key} = $value;
    }
    unset($account->pass);
    if (!$_account
      ->hasPermission('administer users')) {
      unset($account->init);
    }
    if (!$_account
      ->isAuthenticated()) {
      $account->roles = array(
        'anonymous user',
      );
    }
    $json = array(
      'user' => $account,
      'remote_addr' => $_SERVER['REMOTE_ADDR'],
    );
    $response
      ->setContent(json_encode($json));
    $response->headers
      ->set('Content-Type', 'application/json');
    return $response;
  }

  /**
   * Obtain current user data. This is basically copied from D7 Services module.
   * For more info take a look at _system_resource_connect() function (Services).
   */
  public function userSystemResources() {

    // Currently logged in user.
    $output = new \stdClass();

    //$output->sessid = \Drupal::currentUser()->getSessionId();

    //$output->user_name = \Drupal::currentUser()->getSessionData();
    $output->uid = \Drupal::currentUser()
      ->getAccount()
      ->id();
    $output->roles = (object) \Drupal::currentUser()
      ->getRoles();
    return $output;
  }

  /**
   * Returns a collection of variables from the current Drupal site.
   *
   * @return array
   *   Array of variables from the configuration.
   */
  public function drupalgapResourceSystemSiteSettings() {

    // Config names.
    $names = array(
      // @TODO for the moment only working for 'user_register'

      /*'admin_theme',
        'clean_url',
        'date_default_timezone',
        'site_name',
        'theme_default',
        'user_pictures',
        'user_email_verification',*/
      'user.settings' => array(
        'register',
      ),
    );

    // Invoke hook_drupalgap_site_settings() to let others specify variable names
    // to use.
    if (sizeof(\Drupal::moduleHandler()
      ->getImplementations('drupalgap_site_settings')) > 0) {
      \Drupal::moduleHandler()
        ->invokeAll('drupalgap_site_settings', $names);
    }

    // Now fetch the values.
    $settings = new \stdClass();
    foreach ($names as $settingKeys => $settingKey) {
      foreach ($settingKey as $name) {
        $value = \Drupal::config($settingKeys)
          ->get($name);
        $settings->variable = new \stdClass();
        $settings->variable->{$name} = $value;
      }
    }

    // Add Drupal core version into settings.
    $settings->variable->drupal_core = "8";
    return $settings->variable;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
DrupalGapController::drupalgapConfig public function Return the DrupalGap configuration page.
DrupalGapController::drupalgapConnect public function
DrupalGapController::drupalgapResourceSystemSiteSettings public function Returns a collection of variables from the current Drupal site.
DrupalGapController::drupalgapSystemConnect public function
DrupalGapController::drupalStatus public function Checks the current status and add help link.
DrupalGapController::userSystemResources public function Obtain current user data. This is basically copied from D7 Services module. For more info take a look at _system_resource_connect() function (Services).
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.