You are here

public function DrupalGapController::drupalgapConnect in DrupalGap 8.2

See also

https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityMana...

1 string reference to 'DrupalGapController::drupalgapConnect'
drupalgap.routing.yml in ./drupalgap.routing.yml
drupalgap.routing.yml

File

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

Class

DrupalGapController
Returns responses for DrupalGap module routes.

Namespace

Drupal\drupalgap\Controller

Code

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;
}