You are here

swagger_ui_formatter.module in Swagger UI Field Formatter 7

Swagger ui field formatter functionality.

File

swagger_ui_formatter.module
View source
<?php

/**
 * @file
 * Swagger ui field formatter functionality.
 */

/**
 * Implements hook_field_formatter_info().
 */
function swagger_ui_formatter_field_formatter_info() {
  return array(
    'swagger_ui_formatter_swagger_ui' => array(
      'label' => t('Swagger UI Formatter'),
      'description' => t('Formats file fields with Swagger YAML or JSON files with a rendered swagger ui'),
      'field types' => array(
        'file',
      ),
      'settings' => array(
        'validator' => 'default',
        'validator_url' => '',
        'doc_expansion' => 'none',
        'show_request_headers' => FALSE,
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function swagger_ui_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $form = array();
  $form['validator'] = array(
    '#type' => 'select',
    '#title' => t("Validator"),
    '#description' => t("Default=Swagger.io's online validator, None= No validation, Custom=Provide a custom validator url"),
    '#default_value' => $settings['validator'],
    '#options' => array(
      'default' => t('Default'),
      'none' => t('None'),
      'custom' => t('Custom'),
    ),
  );
  $form['validator_url'] = array(
    '#type' => 'textfield',
    '#title' => t("Validator URL"),
    '#description' => t("The custom validator url to be used to validated the swagger files."),
    '#default_value' => $settings['validator_url'],
    '#states' => array(
      'visible' => array(
        ':input[name="fields[' . $field['field_name'] . '][settings_edit_form][settings][validator]"]' => array(
          'value' => 'custom',
        ),
      ),
    ),
  );
  $form['doc_expansion'] = array(
    '#type' => 'select',
    '#title' => t("Doc Expansion"),
    '#description' => t("Controls how the API listing is displayed"),
    '#default_value' => $settings['doc_expansion'],
    '#options' => array(
      'none' => t('Default'),
      'list' => t('List - shows operations for each resource'),
      'full' => t('Fully Expanded - shows operations and their details'),
    ),
  );
  $form['show_request_headers'] = array(
    '#type' => 'checkbox',
    '#title' => t("Show Request Headers"),
    '#default_value' => $settings['show_request_headers'],
    '#description' => t("Whether or not to show the headers that were sent when making a request via the 'Try it out!' option"),
  );
  return $form;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function swagger_ui_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('Uses @validator validator, Doc Expansion of "@doc_expansion" and @request_headers', array(
    '@validator' => $settings['validator'],
    '@doc_expansion' => $settings['doc_expansion'],
    '@request_headers' => $settings['show_request_headers'] ? 'Shows Requests Headers' : 'Request headers are hidden',
  ));
  return $summary;
}

/**
 * Implements hook_field_formatter_view().
 */
function swagger_ui_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $module_path = drupal_get_path('module', 'swagger_ui_formatter');
  $js_path = $module_path . '/swagger_ui/dist/lib/';
  drupal_add_js($js_path . 'jquery-1.8.0.min.js', 'file');
  drupal_add_js($js_path . 'jquery.slideto.min.js', 'file');
  drupal_add_js($js_path . 'jquery.wiggle.min.js', 'file');
  drupal_add_js($js_path . 'jquery.ba-bbq.min.js', 'file');
  drupal_add_js($js_path . 'handlebars-2.0.0.js', 'file');
  drupal_add_js($js_path . 'underscore-min.js', 'file');
  drupal_add_js($js_path . 'backbone-min.js', 'file');
  drupal_add_js($module_path . '/swagger_ui/dist/swagger-ui.js', 'file');
  drupal_add_js($js_path . 'highlight.7.3.pack.js', 'file');
  drupal_add_js($js_path . 'jsoneditor.min.js', 'file');
  drupal_add_js($js_path . 'marked.js', 'file');
  drupal_add_js($js_path . 'swagger-oauth.js', 'file');
  $css_path = $module_path . '/swagger_ui/dist/css/';
  drupal_add_css($css_path . 'typography.css', array(
    'type' => 'file',
    'media' => 'screen',
    'weight' => 200,
    'group' => CSS_THEME,
  ));
  drupal_add_css($css_path . 'reset.css', array(
    'type' => 'file',
    'media' => 'screen',
    'weight' => 201,
    'group' => CSS_THEME,
  ));
  drupal_add_css($css_path . 'screen.css', array(
    'type' => 'file',
    'media' => 'screen',
    'weight' => 202,
    'group' => CSS_THEME,
  ));
  drupal_add_css($css_path . 'reset.css', array(
    'type' => 'file',
    'media' => 'print',
    'weight' => 203,
    'group' => CSS_THEME,
  ));
  drupal_add_css($css_path . 'print.css', array(
    'type' => 'file',
    'media' => 'print',
    'weight' => 204,
    'group' => CSS_THEME,
  ));
  $element = array();
  $settings = $display['settings'];
  $swagger_files = array();
  foreach ($items as $delta => $item) {
    $swagger_files[] = array(
      'url' => file_create_url($item['uri']),
    );
    $element[$delta]['#markup'] = theme('swagger_ui_formatter', array(
      'delta' => $delta,
    ));
  }
  $validator_url = FALSE;
  if ($settings['validator'] == 'none') {
    $validator_url = NULL;
  }
  elseif ($settings['validator'] == 'default') {
    $validator_url = FALSE;
  }
  else {
    $validator_url = $settings['validator_url'];
  }
  drupal_add_js($module_path . '/swagger_ui_formatter.js', 'file');
  drupal_add_js(array(
    'swagger_ui_formatter' => array(
      'swagger_files' => $swagger_files,
      'validator_url' => $validator_url,
      'doc_expansion' => $settings['doc_expansion'],
      'show_request_headers' => $settings['show_request_headers'],
    ),
  ), 'setting');
  return $element;
}

/**
 * Implements hook_theme().
 */
function swagger_ui_formatter_theme($existing, $type, $theme, $path) {
  $path = drupal_get_path('module', 'swagger_ui_formatter') . '/templates';
  return array(
    'swagger_ui_formatter' => array(
      'variables' => array(
        'delta' => NULL,
      ),
      'path' => $path,
      'template' => 'swagger-ui-formatter',
    ),
  );
}