You are here

ds_forms.module in Display Suite 7

Same filename and directory in other branches
  1. 7.2 modules/ds_forms/ds_forms.module

Display Suite forms integration.

File

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

/**
 * @file
 * Display Suite forms integration.
 */

/**
 * Implements hook_menu().
 */
function ds_forms_menu() {
  $items = array();
  if (module_exists('rel')) {
    $items['admin/structure/ds/forms'] = array(
      'title' => 'Forms',
      'description' => 'Displays a list of forms which can be styled.',
      'page callback' => 'drupal_goto',
      'page arguments' => array(
        'admin/structure/rel',
      ),
      'access arguments' => array(
        'admin_display_suite',
      ),
    );
  }
  return $items;
}

/**
 * Implements hook_theme().
 */
function ds_forms_theme() {
  $theme_functions = array();
  $theme_functions['ds_forms_custom_form'] = array(
    'render element' => 'form',
  );
  return $theme_functions;
}

/**
 * Implements hook_ds_layout_info().
 */
function ds_forms_ds_layout_info() {
  $path = drupal_get_path('module', 'ds_forms');
  $layouts = array(
    'ds_form' => array(
      'label' => t('2 column stacked'),
      'path' => $path . '/layouts/ds_form',
      'regions' => array(
        'header' => t('Header'),
        'left' => t('Left'),
        'right' => t('Right'),
        'footer' => t('Footer'),
      ),
      'css' => TRUE,
      'form' => TRUE,
    ),
    'ds_form_3col' => array(
      'label' => t('3 column stacked'),
      'path' => $path . '/layouts/ds_form_3col',
      'regions' => array(
        'header' => t('Header'),
        'left' => t('Left'),
        'middle' => t('Middle'),
        'right' => t('Right'),
        'footer' => t('Footer'),
      ),
      'css' => TRUE,
      'form' => TRUE,
    ),
  );
  return $layouts;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ds_forms_form_field_ui_field_overview_form_alter(&$form, &$form_state) {

  // We currently only work on node forms or custom forms.
  if ($form['#entity_type'] == 'node' || $form['#entity_type'] == 'rel_build') {

    // Add necessary variables for DS Field UI.
    $form['#view_mode'] = 'form';
    $form_state['no_panels'] = TRUE;
    $form_state['no_view_mode_suggestions'] = TRUE;

    // Make sure the refresh works.
    if (!module_exists('field_group') && !module_exists('rel')) {

      // This key is used to store the current updated field.
      $form_state += array(
        'formatter_settings_edit' => NULL,
      );

      // Add AJAX wrapper.
      $form['fields']['#prefix'] = '<div id="field-display-overview-wrapper">';
      $form['fields']['#suffix'] = '</div>';

      // See field_ui.admin.inc for more details on refresh rows.
      $form['refresh_rows'] = array(
        '#type' => 'hidden',
      );
      $form['refresh'] = array(
        '#type' => 'submit',
        '#value' => t('Refresh'),
        '#op' => 'refresh_table',
        '#submit' => array(
          'field_ui_display_overview_multistep_submit',
        ),
        '#ajax' => array(
          'callback' => 'field_ui_display_overview_multistep_js',
          'wrapper' => 'field-display-overview-wrapper',
          'effect' => 'fade',
          // The button stays hidden, so we hide the AJAX spinner too. Ad-hoc
          // spinners will be added manually by the client-side script.
          'progress' => 'none',
        ),
      );
      $form['#attached']['css'][] = drupal_get_path('module', 'ds_forms') . '/ds_forms.admin.css';
    }

    // Attach js.
    $form['#attached']['js'][] = drupal_get_path('module', 'ds_forms') . '/ds_forms.admin.js';

    // Load Display suite.
    form_load_include($form_state, 'inc', 'ds', 'ds.field_ui');
    ds_field_ui_fields_layouts($form, $form_state);
  }
}

/**
 * Implements hook_form_alter().
 */
function ds_forms_form_alter(&$form, &$form_state, $form_id) {

  // Form layout.
  if ($ds_form = ds_build_load($form, $form_id)) {
    if ($layout = ds_get_layout($ds_form->entity_type, $ds_form->bundle, 'form', FALSE)) {

      // Add the theming function and add the layout as a class.
      $form['#theme'] = array(
        'ds_forms_custom_form',
      );
      $class = strtr($layout['layout'], '_', '-');
      if (isset($form['#attributes']['class']) && is_array($form['#attributes']['class']) || !isset($form['#attributes']['class'])) {
        $form['#attributes']['class'][] = $class;
      }
      elseif (isset($form['#attributes']['class']) && is_string($form['#attributes']['class'])) {
        $form['#attributes']['class'] .= ' ' . $class . ' ';
      }
    }
  }
}

/**
 * Helper function to determine if this form can be loaded.
 */
function ds_build_load($form, $form_id) {
  $ds_form = FALSE;
  if (module_exists('rel')) {
    $ds_form = rel_build_load($form_id);
  }
  if (!$ds_form && isset($form['#entity_type']) && isset($form['#bundle']) && $form_id != 'field_ui_field_overview_form') {
    $ds_form = new stdClass();
    $ds_form->entity_type = $form['#entity_type'];
    $ds_form->bundle = $form['#bundle'];
    $ds_form->rel_type = 'form';
  }
  return $ds_form;
}

/**
 * Implements hook_preprocess_ds_forms_custom_form().
 */
function ds_forms_preprocess_ds_forms_custom_form(&$vars) {
  $rel_build = ds_build_load($vars['form'], $vars['form']['#form_id']);
  $entity_type = $rel_build->entity_type;
  $bundle = $rel_build->bundle;
  if ($vars['form']['#form_id'] == 'field_ui_display_overview_form') {
    return;
  }
  if ($layout = ds_get_layout($entity_type, $bundle, 'form', FALSE)) {

    // Hide empty regions variable.
    $hide_empty_regions = $layout['settings']['hide_empty_regions'];

    // Theme hook suggestions.
    $vars['theme_hook_suggestions'][] = $layout['layout'];
    $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $entity_type;
    $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $entity_type . '_' . $bundle;
    $form =& $vars['form'];

    // Add path to css file.
    if (isset($layout['css'])) {
      drupal_add_css($layout['path'] . '/' . $layout['layout'] . '.css');
    }

    // Add the hidden region.
    $layout['regions']['hidden'] = 'Hidden';

    // Create region variables based on the layout settings.
    foreach ($layout['regions'] as $region_name => $region) {

      // Create the region content.
      if ($region_name == 'hidden') {
        ds_forms_render_region($form, $region_name, $layout);
      }
      else {
        $vars[$region_name] = ds_forms_render_region($form, $region_name, $layout);

        // Hide empty regions.
        if ($hide_empty_regions && empty($vars[$region_name]) && empty($layout['flexible'])) {
          $vars[$region_name] = FALSE;
        }
        elseif (empty($vars[$region_name])) {
          $vars[$region_name] = '&nbsp;';
        }
      }

      // Add extras classes to the region.
      $vars[$region_name . '_classes'] = !empty($layout['settings']['classes'][$region_name]) ? ' ' . implode(' ', $layout['settings']['classes'][$region_name]) : '';
    }
  }
}

/**
 * Render a form region.
 *
 * @param $content
 *   An array of content fields.
 * @param $region
 *   The name of region to render.
 * @param $layout
 *   The layout definition.
 */
function ds_forms_render_region(&$content, $region, $layout) {
  $output = '';
  if (isset($layout['settings']['regions'][$region])) {
    foreach ($layout['settings']['regions'][$region] as $key => $field) {
      if ($region == 'hidden') {
        $content[$field]['#access'] = FALSE;
      }
      else {
        $output .= drupal_render($content[$field]);
      }
    }
  }
  return $output;
}

/**
 * Implements hook_rel_build_unregister().
 */
function ds_forms_rel_build_unregister($build) {
  if ($build->entity_type == 'rel_build') {
    $layout_id = $build->entity_type . '|' . $build->bundle . '|form';
    db_delete('ds_layout_settings')
      ->condition('id', $layout_id)
      ->execute();
  }
}

Functions

Namesort descending Description
ds_build_load Helper function to determine if this form can be loaded.
ds_forms_ds_layout_info Implements hook_ds_layout_info().
ds_forms_form_alter Implements hook_form_alter().
ds_forms_form_field_ui_field_overview_form_alter Implements hook_form_FORM_ID_alter().
ds_forms_menu Implements hook_menu().
ds_forms_preprocess_ds_forms_custom_form Implements hook_preprocess_ds_forms_custom_form().
ds_forms_rel_build_unregister Implements hook_rel_build_unregister().
ds_forms_render_region Render a form region.
ds_forms_theme Implements hook_theme().