You are here

function matrix_field_settings in Matrix field 6.2

Same name and namespace in other branches
  1. 5 matrix.module \matrix_field_settings()
  2. 6 matrix.module \matrix_field_settings()

Implementation of hook_field_settings().

File

./matrix.module, line 107
Defines simple matrix field types.

Code

function matrix_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      drupal_add_js(drupal_get_path('module', 'matrix') . '/matrix.js');
      drupal_add_css(drupal_get_path('module', 'matrix') . '/matrix.css');

      //prep the cache with the form elements
      cache_set('matrix-rows-' . $field['field_name'], unserialize(str_replace("\r", "", $field['rows_elements'])));
      cache_set('matrix-cols-' . $field['field_name'], unserialize(str_replace("\r", "", $field['cols_elements'])));
      $mode = !empty($field['mode']) ? $field['mode'] : 'cols';
      $empty = !empty($field['empty']) ? $field['empty'] : '-';
      $empty_hide = $field['empty_hide'];

      // Field Information
      $form['info'] = array(
        '#tree' => TRUE,
        'field_name' => array(
          '#type' => 'hidden',
          '#value' => $field['field_name'],
        ),
        'field_type' => array(
          '#type' => 'hidden',
          '#value' => $field['widget']['type'],
        ),
      );

      // Config Settings
      $form['mode'] = array(
        '#type' => 'radios',
        '#title' => t('Rows or columns?'),
        '#options' => array(
          'cols' => t('Columns define element types'),
          'rows' => t('Rows define element types'),
        ),
        '#default_value' => $mode,
        '#description' => t('You can define element types (eg text fields, select boxes) for each cell in the grid.  Choose if the columns or the rows will decide the element types'),
      );
      $form['empty'] = array(
        '#type' => 'textfield',
        '#title' => t('Empty cell'),
        '#size' => 2,
        '#default_value' => $empty,
        '#description' => t('Place holder for blank cells'),
      );
      $form['empty_hide'] = array(
        '#type' => 'checkbox',
        '#title' => t('Hide empty cells'),
        '#default_value' => $empty_hide,
        '#description' => t('Tick to hide empty cells from the display'),
      );
      $form['cols'] = array(
        '#type' => 'fieldset',
        '#title' => t('Columns'),
      );
      $form['cols']['list'] = array(
        '#type' => 'markup',
        '#value' => matrix_settings_default('list', $field['field_name'], 'cols', $field['cols_elements']),
        '#prefix' => '<div id="edit-cols-list">',
        '#suffix' => '</div>',
      );
      $form['cols']['add_cols'] = array(
        '#type' => 'button',
        '#value' => t('Add new column'),
        '#attributes' => array(
          'class' => 'matrix-cols',
        ),
      );
      $form['cols']['throbber'] = array(
        '#type' => 'markup',
        '#value' => '<div id="matrix-cols-throbber"></div>',
      );
      $form['cols']['cols_elements'] = array(
        '#type' => 'hidden',
        '#default_value' => matrix_settings_default('data', $field['field_name'], 'cols', $field['cols_elements']),
      );
      $form['rows'] = array(
        '#type' => 'fieldset',
        '#title' => t('Rows'),
      );
      $form['rows']['list'] = array(
        '#type' => 'markup',
        '#value' => matrix_settings_default('list', $field['field_name'], 'rows', $field['rows_elements']),
        '#prefix' => '<div id="edit-rows-list">',
        '#suffix' => '</div>',
      );
      $form['rows']['add_rows'] = array(
        '#type' => 'button',
        '#value' => t('Add new row'),
        '#attributes' => array(
          'class' => 'matrix-rows',
        ),
      );
      $form['rows']['throbber'] = array(
        '#type' => 'markup',
        '#value' => '<div id="matrix-rows-throbber"></div>',
      );
      $form['rows']['rows_elements'] = array(
        '#type' => 'hidden',
        '#default_value' => matrix_settings_default('data', $field['field_name'], 'rows', $field['rows_elements']),
      );
      $form['preview'] = array(
        '#type' => 'fieldset',
        '#title' => t('Preview'),
      );
      $form['preview']['container'] = array(
        '#type' => 'markup',
        '#value' => drupal_get_form('matrix_settings_preview', $field['field_name'], $mode, unserialize(str_replace("\r", "", $field['rows_elements'])), unserialize(str_replace("\r", "", $field['cols_elements']))),
        '#prefix' => '<div id="matrix-preview">',
        '#suffix' => '</div>',
      );
      return $form;
    case 'save':
      cache_clear_all('matrix-rows-' . $field['field_name'], 'cache');
      cache_clear_all('matrix-cols-' . $field['field_name'], 'cache');
      $values[] = 'mode';
      $values[] = 'empty';
      $values[] = 'empty_hide';
      $values[] = 'rows_elements';
      $values[] = 'cols_elements';
      return $values;
    case 'validate':
      break;
  }
}