You are here

function restful_angular_example_page in RESTful 7

Page callback; Load the AngularJs form.

1 string reference to 'restful_angular_example_page'
restful_angular_example_menu in modules/restful_angular_example/restful_angular_example.module
Implements hook_menu().

File

modules/restful_angular_example/restful_angular_example.module, line 205
Example module for the RESTful AngularJs module.

Code

function restful_angular_example_page($example) {
  $controller_name = preg_replace('/[-_]/', ' ', $example) . ' Ctrl';
  $controller_name = ucwords($controller_name);
  $controller_name = preg_replace('/ /', '', $controller_name);
  if ($example == 'form') {

    // Pass info via Drupal.settings.
    $settings['restfulExample'] = array(
      'basePath' => url('', array(
        'absolute' => TRUE,
      )) . '/',
      'csrfToken' => drupal_get_token(\RestfulInterface::TOKEN_VALUE),
      'data' => array(
        'article' => array(
          'label' => 'no',
        ),
      ),
    );
    drupal_add_js($settings, 'setting');

    // This is a big piece of hack, since Drupal.settings is not available when
    // adding Angular. This is because of the JS_LIBRARY group against the hard
    // coded group in core to add the settings. The alternative is add a global
    // variable in the window object that contains the information we want to
    // pass to the angular module constructor.
    drupal_add_js("window.restfulExampleModules = ['angularFileUpload', 'ngPrettyJson', 'ui.select']", array(
      'type' => 'inline',
      // Make sure it is available when constructing the angular module.
      'scope' => 'header',
      'group' => JS_LIBRARY - 10,
    ));

    // Theme function simply declares the angular app, and ng-includes the app's
    // view.
    $app_path = drupal_get_path('module', 'restful_angular_example') . '/components/restful-app/dist';
    $url = url($app_path . '/views/form.html', array(
      'absolute' => TRUE,
      'language' => LANGUAGE_NONE,
    ));

    // Add the library.
    drupal_add_library('restful_angular_example', 'restful-angular-form');
  }
  if ($example == 'admin') {

    // Pass info via Drupal.settings.
    $settings['restfulExample'] = array(
      'apiPath' => url(variable_get('restful_hook_menu_base_path', 'api'), array(
        'absolute' => TRUE,
      )),
      'apiVersion' => 'v1.5',
      'csrfToken' => drupal_get_token(\RestfulInterface::TOKEN_VALUE),
      'data' => array(
        'article' => array(
          'label' => 'no',
        ),
      ),
    );
    drupal_add_js($settings, 'setting');
    drupal_add_js("window.restfulExampleModules = ['ng-admin', 'restangular']", array(
      'type' => 'inline',
      // Make sure it is available when constructing the angular module.
      'scope' => 'header',
      'group' => JS_LIBRARY - 10,
    ));

    // Theme function simply declares the angular app, and ng-includes the app's
    // view.
    $app_path = drupal_get_path('module', 'restful_angular_example') . '/components/restful-app/dist';
    $url = url($app_path . '/views/admin.html', array(
      'absolute' => TRUE,
      'language' => LANGUAGE_NONE,
    ));

    // Add the library.
    drupal_add_library('restful_angular_example', 'restful-angular-admin');
  }
  if (empty($url)) {
    return t('Example not found.');
  }
  return theme('restful_angular_example_angular', array(
    'url' => $url,
    'controller' => $controller_name,
  ));
}