You are here

function drupalgap_connect in DrupalGap 7.2

Used to retrieve Drupal configuration settings as JSON to fuel the app.

1 string reference to 'drupalgap_connect'
drupalgap_menu in ./drupalgap.module
Implements hook_menu().

File

./drupalgap.module, line 231
A module to provide a bridge between Drupal websites and PhoneGap mobile applications.

Code

function drupalgap_connect() {
  $result = new stdClass();

  // Grab the date formats and types. For each date type, look in the variable
  // table to find its format. Not all types will have a format.
  $result->date_formats = system_get_date_formats();
  $result->date_types = system_get_date_types();
  foreach ($result->date_types as $name => $type) {
    $value = variable_get('date_format_' . $name, false);
    if ($value) {
      $result->date_types[$name]['format'] = $value;
    }
  }

  // Grab some basic site settings.
  module_load_include('inc', 'drupalgap', 'drupalgap.resource');
  $result->site_settings = _drupalgap_resource_system_site_settings();

  // Grab all field information.
  $result->field_info_fields = field_info_fields();

  // For each entity type, expose their field info instances, field info extra
  // fields and a trimmed down version of the entity_get_info().
  $result->entity_info = array();
  $result->field_info_extra_fields = array();
  foreach (entity_get_info() as $entity_type => $entity) {
    $result->field_info_instances[$entity_type] = field_info_instances($entity_type);
    $result->field_info_extra_fields[$entity_type] = array();
    $_entity_info = entity_get_info($entity_type);
    $entity_info = array(
      'bundles' => array(),
      'entity_keys' => $_entity_info['entity keys'],
      'label' => $_entity_info['label'],
      'base_table' => $_entity_info['schema_fields_sql']['base table'],
      'base_table_field_types' => $_entity_info['base table field types'],
    );

    // @TODO this may be provided by the Entity module, and not part of Drupal core.
    if (isset($_entity_info['plural label'])) {
      $entity_info['plural_label'] = $_entity_info['plural label'];
    }
    foreach ($_entity_info['bundles'] as $bundle => $_bundle) {
      $result->field_info_extra_fields[$entity_type][$bundle] = array(
        'form' => field_info_extra_fields($entity_type, $bundle, 'form'),
        'display' => field_info_extra_fields($entity_type, $bundle, 'display'),
      );
      $entity_info['bundles'][$bundle] = array(
        'bundle' => $bundle,
        'label' => $_bundle['label'],
      );

      // @TODO this may be provided by the Entity module, and not part of Drupal core.
      if (isset($_bundle['admin']['access arguments'])) {
        $entity_info['bundles'][$bundle]['access_arguments'] = $_bundle['admin']['access arguments'];
      }
    }
    $result->entity_info[$entity_type] = $entity_info;
  }

  //$result->_entity_info = entity_get_info(); // uncomment to see complete entity info, don't use in production

  // @NOTE - manually set the "entity keys" 'bundle' and 'label' for users
  // because it isn't set by Drupal for whatever reason(s).

  //$result->entity_info['user']['entity_keys']['bundle'] = 'user';

  //$result->entity_info['taxonomy_vocabulary']['entity_keys']['bundle'] = 'taxonomy_vocabulary';
  $result->entity_info['user']['entity_keys']['label'] = 'name';

  // Finally, spit out the json.
  drupal_json_output($result);
}