You are here

restful_angular_example.module in RESTful 7

Example module for the RESTful AngularJs module.

Demonstrate the use of AngularJs forms instead of Form API, along with RESTful endpoint and the entity validator module.

File

modules/restful_angular_example/restful_angular_example.module
View source
<?php

/**
 * @file
 * Example module for the RESTful AngularJs module.
 *
 * Demonstrate the use of AngularJs forms instead of Form API, along with
 * RESTful endpoint and the entity validator module.
 */

/**
 * Implements hook_menu().
 */
function restful_angular_example_menu() {
  $items['restful-example/%'] = array(
    'title' => 'AngularJs example',
    'access callback' => 'node_access',
    'access arguments' => array(
      'create',
      'article',
    ),
    'page callback' => 'restful_angular_example_page',
    'page arguments' => array(
      1,
    ),
    'delivery callback' => 'restful_angular_example_delivery',
  );
  return $items;
}

/**
 * Custom delivery callback.
 *
 * Used to override html.tpl.php from the module.
 */
function restful_angular_example_delivery($page_callback_result) {
  global $language;

  // Pass variables to the template.
  $vars = array(
    'language' => $language,
    'head_title' => drupal_get_title(),
    'favicon' => '',
    'styles' => drupal_get_css(),
    'scripts' => drupal_get_js(),
    'messages' => drupal_get_messages(),
    'content' => $page_callback_result,
  );
  echo theme('restful_angular_example_html', $vars);
  drupal_page_footer();
}

/**
 * Implements hook_theme().
 */
function restful_angular_example_theme() {
  $theme['restful_angular_example_angular'] = array(
    'template' => 'restful-angular',
    'path' => drupal_get_path('module', 'restful_angular_example') . '/templates',
    'variables' => array(
      'url' => NULL,
      'controller' => NULL,
    ),
  );
  $theme['restful_angular_example_html'] = array(
    'template' => 'restful-angular-html',
    'path' => drupal_get_path('module', 'restful_angular_example') . '/templates',
    'variables' => array(
      'language' => NULL,
      'head_title' => NULL,
      'favicon' => NULL,
      'styles' => NULL,
      'scripts' => NULL,
      'messages' => NULL,
      'content' => NULL,
    ),
  );
  return $theme;
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function restful_angular_example_ctools_plugin_directory($module, $plugin) {
  if ($module == 'restful') {
    return 'plugins/' . $plugin;
  }
}

/**
 *  Implements hook_library().
 */
function restful_angular_example_library() {
  $bower_path = drupal_get_path('module', 'restful_angular_example') . '/components/restful-app/bower_components';

  // AngularJS library.
  $libraries['angular'] = array(
    'title' => t('AngularJS'),
    'version' => '1.2.27',
    'website' => 'https://github.com/angular/angular.js',
    'js' => array(
      $bower_path . '/angular/angular.js' => array(),
    ),
  );

  // JSON pretty print library.
  $libraries['json-pretty-print'] = array(
    'title' => t('JSON pretty print'),
    'version' => '0.1.1',
    'website' => 'https://github.com/darul75/ng-prettyjson',
    'js' => array(
      $bower_path . '/ng-prettyjson/dist/ng-prettyjson.min.js' => array(),
    ),
    'css' => array(
      $bower_path . '/ng-prettyjson/dist/ng-prettyjson.min.css' => array(),
    ),
    'dependencies' => array(
      array(
        'restful_angular_example',
        'angular',
      ),
    ),
  );

  // Select2 library.
  $libraries['select2'] = array(
    'title' => t('Select 2'),
    'version' => '0.0.5',
    'website' => 'https://github.com/angular-ui/ui-select',
    'js' => array(
      $bower_path . '/ui-select/dist/select.min.js' => array(),
    ),
    'css' => array(
      $bower_path . '/ui-select/dist/select.min.css' => array(),
    ),
    'dependencies' => array(
      array(
        'restful_angular_example',
        'angular',
      ),
    ),
  );

  // File upload library.
  $libraries['file-upload'] = array(
    'title' => t('File upload'),
    'version' => '2.0.0',
    'website' => 'https://github.com/danialfarid/angular-file-upload',
    'js' => array(
      $bower_path . '/danialfarid-angular-file-upload/dist/angular-file-upload-shim.min.js' => array(),
      $bower_path . '/danialfarid-angular-file-upload/dist/angular-file-upload.min.js' => array(),
    ),
    'dependencies' => array(
      array(
        'restful_angular_example',
        'angular',
      ),
    ),
  );

  // ng-admin library.
  $libraries['ng-admin'] = array(
    'title' => t('ng-admin'),
    'version' => '0.3.3',
    'website' => 'https://github.com/marmelab/ng-admin',
    'js' => array(
      $bower_path . '/ng-admin/build/ng-admin.min.js' => array(),
    ),
    'css' => array(
      $bower_path . '/ng-admin/build/ng-admin.min.css' => array(),
    ),
    'dependencies' => array(
      array(
        'restful_angular_example',
        'angular',
      ),
    ),
  );

  // Load our custom app.
  $app_path = drupal_get_path('module', 'restful_angular_example') . '/components/restful-app/dist';

  // Custom libraries.
  $libraries['restful-angular-form'] = array(
    'title' => t('RESTful Angular Form'),
    'version' => '1.0.0',
    'website' => 'https://github.com/Gizra/restful/blob/7.x-1.x/modules/restful_angular_example/README.md',
    'js' => array(
      $app_path . '/restful-app.js' => array(),
    ),
    'css' => array(
      $app_path . '/css/restful-app.css' => array(),
    ),
    'dependencies' => array(
      array(
        'restful_angular_example',
        'json-pretty-print',
      ),
      array(
        'restful_angular_example',
        'select2',
      ),
      array(
        'restful_angular_example',
        'file-upload',
      ),
    ),
  );
  $libraries['restful-angular-admin'] = array(
    'title' => t('RESTful Angular Admin'),
    'version' => '1.0.0',
    'website' => 'https://github.com/Gizra/restful/blob/7.x-1.x/modules/restful_angular_example/README.md',
    'js' => array(
      $app_path . '/restful-app.js' => array(),
    ),
    'css' => array(
      $app_path . '/css/restful-app.css' => array(),
    ),
    'dependencies' => array(
      array(
        'restful_angular_example',
        'ng-admin',
      ),
    ),
  );
  return $libraries;
}

/**
 * Page callback; Load the AngularJs form.
 */
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,
  ));
}