You are here

ds.buildmodes.inc in Display Suite 6.2

Same filename and directory in other branches
  1. 6.3 includes/ds.buildmodes.inc
  2. 6 includes/ds.buildmodes.inc

Manage build modes.

File

includes/ds.buildmodes.inc
View source
<?php

/**
 * @file
 * Manage build modes.
 */

/**
 * Build modes overview.
 */
function ds_build_modes_page($module) {
  $output = '';
  $action = check_plain(arg(6));
  $build_mode = check_plain(arg(7));
  $build_modes = ds_get_build_modes($module, TRUE);
  $api_info = ds_api_info($module);
  $hide = FALSE;

  // Only show edit forms if allowed by api
  if (!isset($api_info['no_new_build_modes'])) {

    // Delete form.
    if (in_array($build_mode, array_keys($build_modes)) && $action == 'delete') {
      $output .= drupal_get_form('ds_build_mode_delete_form', $module, $build_mode);
      $hide = TRUE;
    }
    elseif (in_array($build_mode, array_keys($build_modes)) && $action == 'edit') {
      $output .= drupal_get_form('ds_build_mode_form', $module, $build_mode);
      $hide = TRUE;
    }
    if (empty($build_modes)) {
      $output .= '<p>' . t('You have not defined any custom build modes.') . '</p>';
    }
  }
  if ($hide == FALSE) {

    // Custom build modes overview form.
    if (!empty($build_modes)) {
      $groups = array();
      foreach ($build_modes as $key => $build_mode) {
        $groups[$build_mode['group']][$key] = $build_mode;
      }
      $header = array(
        t('Group'),
        t('Name'),
        t('Machine name'),
        t('Module'),
        t('State'),
        t('Operations'),
      );
      $rows = array();
      foreach ($groups as $group_key => $group) {

        // Add a header row for this group
        $rows[] = array(
          'data' => array(
            array(
              'data' => '<em>' . $group_key . '</em>',
              'colspan' => 6,
            ),
          ),
        );
        foreach ($group as $key => $mode) {

          // Check state of build mode
          $in_code = TRUE;
          $overridden = FALSE;
          $state = 'default';
          $state_message = t('In code');
          if (!isset($mode['module']) || empty($mode['module'])) {
            $in_code = FALSE;
            $state_message = t('Database');
            $state = 'custom';
          }
          elseif (isset($mode['overridden']) && !empty($mode['overridden'])) {
            $overridden = TRUE;
            $state_message = t('Overridden');
            $state = 'overridden';
          }

          // Build the row
          $row = array();
          $row[] = '';
          $row[] = '<strong>' . check_plain($mode['title']) . '</strong>';
          $row[] = $key;
          $row[] = isset($mode['module']) ? $mode['module'] : 'unknown';
          $row[] = $state_message;
          if ($state != 'default') {
            $row[] = l(t('Edit'), DS_PATH_MODULES . '/' . $module . '/buildmodes/edit/' . $key) . ' - ' . l(t('Delete'), DS_PATH_MODULES . '/' . $module . '/buildmodes/delete/' . $key);
          }
          else {
            $row[] = '';
          }
          $rows[] = array(
            'data' => $row,
          );
        }
      }
      $output .= theme('table', $header, $rows);
    }

    // New build mode form.
    if (!isset($api_info['no_new_build_modes'])) {
      $output .= drupal_get_form('ds_build_mode_form', $module);
    }
    else {
      $output .= theme('fieldset', array(
        '#title' => t('Add new build mode'),
        '#value' => '<p>This type does not support custom build modes.</p>',
      ));
    }

    // Matrix form.
    if (isset($api_info['types'])) {
      $types = $api_info['types']();
      $output .= drupal_get_form('ds_buildmodes_matrix_form', $module, $types);
    }
  }
  return $output;
}

/**
 * Build mode form.
 *
 * @param string $form_state
 *  The form
 * @param string $module
 *  The name of the module.
 * @param string $build_mode
 *  The name of the build mode.
 */
function ds_build_mode_form($form_state, $module, $build_mode = '') {
  $form = array();
  if (!empty($build_mode)) {
    $build_modes = variable_get($module . '_build_modes', array());
    $title = $build_modes[$build_mode];
  }
  else {
    $title = '';
  }
  $form['identity'] = array(
    '#type' => 'fieldset',
    '#title' => empty($build_mode) ? t('Add new build mode') : t('Update build mode'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $form['identity']['key'] = array(
    '#type' => 'textfield',
    '#title' => 'Key name',
    '#description' => t('The machine-readable name of this build mode.'),
    '#required' => TRUE,
    '#maxlength' => 32,
  );
  if (!empty($build_mode)) {
    $form['identity']['key']['#disabled'] = TRUE;
    $form['identity']['key']['#value'] = $build_mode;
    $form['identity']['key']['#description'] = t('The machine-readable name of this build mode. Note: you can not edit this key.');
  }
  $form['identity']['name'] = array(
    '#type' => 'textfield',
    '#title' => 'Display name',
    '#description' => t('The name of this build mode which will be used for every type on the display fields page.'),
    '#required' => TRUE,
    '#default_value' => $title,
  );
  $form['identity']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save build mode'),
    '#submit' => array(
      'ds_build_mode_form_submit',
    ),
  );
  $form['#module'] = $module;
  $form['#form_type'] = empty($build_mode) ? 'insert' : 'update';
  return $form;
}

/**
 * Validate new custom build mode submission.
 */
function ds_build_mode_form_validate($form, &$form_state) {
  if ($form['#form_type'] == 'insert') {
    $existing = array_keys(ds_get_build_modes($form['#module']));
    if (in_array($form_state['values']['key'], $existing)) {
      form_set_error('key', t('This build mode already exists.'));
    }
    if (!preg_match('!^[a-z_0-9]+$!', $form_state['values']['key'])) {
      form_set_error('key', t('The machine-readable name must contain only lowercase letters, numbers and underscores.'));
    }
  }
}

/**
 * Save new build mode.
 */
function ds_build_mode_form_submit($form, &$form_state) {
  $build_modes = variable_get($form['#module'] . '_build_modes', array());
  $build_modes[$form_state['values']['key']] = $form_state['values']['name'];
  variable_set($form['#module'] . '_build_modes', $build_modes);
  drupal_set_message(t('Build mode %build_mode has been saved.', array(
    '%build_mode' => $build_modes[$form_state['values']['key']],
  )));
  $form_state['redirect'] = DS_PATH_MODULES . '/' . $form['#module'] . '/buildmodes';
  menu_rebuild();
  drupal_rebuild_theme_registry();
}

/**
 * Build mode delete form.
 *
 * @param string $module The module name.
 * @param string $build_mode The build mode to delete.
 */
function ds_build_mode_delete_form($form_state, $module, $build_mode) {
  $form = array();
  $form['question'] = array(
    '#type' => 'markup',
    '#value' => '<p>' . t('Are you sure you want to delete the build mode %build_mode ?', array(
      '%build_mode' => $build_mode,
    )) . '</p>',
  );
  $form['display'] = array(
    '#type' => 'value',
    '#value' => $build_mode,
  );
  $form['button']['submit'] = array(
    '#prefix' => '<div>',
    '#type' => 'submit',
    '#value' => t('Delete'),
  );
  $form['buttons']['cancel'] = array(
    '#suffix' => '</div>',
    '#type' => 'markup',
    '#value' => l('cancel', DS_PATH_MODULES . '/' . $module . '/buildmodes'),
  );
  $form['#module'] = $module;
  return $form;
}

/**
 * Delete build mode.
 */
function ds_build_mode_delete_form_submit($form, &$form_state) {
  $module = $form['#module'];
  $build_modes = variable_get($module . '_build_modes', array());
  unset($build_modes[$form_state['values']['display']]);
  variable_set($module . '_build_modes', $build_modes);

  // Remove any settings of build modes from the database.
  db_query("DELETE FROM {ds_settings} WHERE build_mode = '%s' AND module = '%s'", $form_state['values']['display'], $module);
  drupal_set_message(t('Custom build mode %build_mode has been removed.', array(
    '%build_mode' => $form_state['values']['display'],
  )));
  $form_state['redirect'] = DS_PATH_MODULES . '/' . $module . '/buildmodes';
  menu_rebuild();
  drupal_rebuild_theme_registry();
}

/**
 * Build modes matrix form.
 *
 * @param string $module The name of the module.
 * @param array $types A list of types.
 */
function ds_buildmodes_matrix_form($form_state, $module, $types) {
  drupal_add_js(drupal_get_path('module', 'ds') . '/js/buildmodes.js');
  if (is_array($types) && !empty($types)) {

    // Exclude build mode matrix.
    $exclude_build_modes = variable_get($module . '_buildmodes_exclude', array());
    $all_build_modes = ds_get_build_modes($module);
    $build_modes = array(
      'ds_block_all' => array(
        'title' => t('All'),
      ),
    );
    if (count($all_build_modes) > 1) {
      $build_modes += $all_build_modes;
    }
    $form['#build_modes'] = $build_modes;
    $form['#types'] = $types;
    $form['#module'] = $module;
    foreach ($types as $tkey => $type) {
      $row_exluded = variable_get($module . '_type_' . $tkey, FALSE);
      foreach ($build_modes as $bkey => $mode) {

        // We create 2 form keys here. The first one is the checkbox,
        // the second is a hidden value to store the default value in.
        $form['exclude'][$tkey][$tkey . '-' . $bkey] = array(
          '#type' => 'checkbox',
          '#attributes' => array(
            'class' => 'checkbox-instance',
          ),
        );
        $form['exclude'][$tkey][$tkey . '-' . $bkey . '-disabled'] = array(
          '#type' => 'value',
          '#value' => isset($exclude_build_modes[$tkey][$bkey]) ? $exclude_build_modes[$tkey][$bkey] : NULL,
        );
        if ($bkey == 'ds_block_all') {
          $form['exclude'][$tkey][$tkey . '-' . $bkey]['#attributes']['class'] .= ' block-all';
          $form['exclude'][$tkey][$tkey . '-' . $bkey]['#default_value'] = $row_exluded;
        }
        else {
          $form['exclude'][$tkey][$tkey . '-' . $bkey]['#default_value'] = isset($exclude_build_modes[$tkey][$bkey]) ? $exclude_build_modes[$tkey][$bkey] : NULL;
          if ($row_exluded) {
            $form['exclude'][$tkey][$tkey . '-' . $bkey]['#disabled'] = 'disabled';
          }
        }
      }
    }
    $form['exclude']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save settings'),
      '#submit' => array(
        'ds_build_modes_matrix_submit',
      ),
    );
  }
  return $form;
}

/**
 * Submit build mode matrix form.
 */
function ds_build_modes_matrix_submit($form, &$form_state) {
  $exclude_build_modes = array();
  foreach ($form['#types'] as $ckey => $type) {
    $global_exclude = FALSE;
    foreach ($form['#build_modes'] as $bkey => $mode) {

      // Global exclude.
      if ($bkey == 'ds_block_all') {
        $global_exclude = $form_state['values'][$ckey . '-' . $bkey];
        variable_set($form['#module'] . '_type_' . $ckey, $form_state['values'][$ckey . '-' . $bkey]);
        $exclude_build_modes[$ckey][$bkey] = $form_state['values'][$ckey . '-' . $bkey];
      }
      else {
        $value = $global_exclude == TRUE ? $form_state['values'][$ckey . '-' . $bkey . '-disabled'] : $form_state['values'][$ckey . '-' . $bkey];
        $exclude_build_modes[$ckey][$bkey] = $value;
      }
    }
  }
  variable_set($form['#module'] . '_buildmodes_exclude', $exclude_build_modes);
  drupal_set_message(t('Disable type/build mode settings have been saved.'));
  menu_rebuild();
  drupal_rebuild_theme_registry();
}

Functions

Namesort descending Description
ds_buildmodes_matrix_form Build modes matrix form.
ds_build_modes_matrix_submit Submit build mode matrix form.
ds_build_modes_page Build modes overview.
ds_build_mode_delete_form Build mode delete form.
ds_build_mode_delete_form_submit Delete build mode.
ds_build_mode_form Build mode form.
ds_build_mode_form_submit Save new build mode.
ds_build_mode_form_validate Validate new custom build mode submission.