You are here

function matrix_field_widget_form in Matrix field 8.2

Same name and namespace in other branches
  1. 7.2 matrix.module \matrix_field_widget_form()

Implements hook_field_widget_form().

File

./matrix.module, line 166
Contains matrix.module.

Code

function matrix_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $field_name = $field['field_name'];
  $cols_count = $field['settings']['cols_count'];
  $rows_count = $field['settings']['rows_count'];
  $default_values = array();
  foreach ($items as $delta => $item) {
    if ($item && !empty($item['row'])) {
      $default_values[$item['row']][$item['col']][] = $item['value'];
    }
  }
  switch ($field['type']) {
    case 'matrix_text':
      $element['#matrix_rows'] = $rows_count;
      $element['#matrix_cols'] = $cols_count;
      $element['#matrix_more_rows'] = FALSE;
      $element['#matrix_more_cols'] = FALSE;
      $size = $field['settings']['size'];
      $spreadsheet_style = $field['settings']['spreadsheet_style'];
      if ($spreadsheet_style) {
        for ($row = 1; $row <= $rows_count; $row++) {
          $row_labels[$row] = $row;
        }
        for ($col = 1; $col <= $cols_count; $col++) {
          $column_labels[$col] = matrix_make_letter($col);
        }
      }
      else {
        $row_labels = array();
        $column_labels = array();
      }
      for ($row = 1; $row <= $rows_count; $row++) {
        for ($col = 1; $col <= $cols_count; $col++) {
          $element['grid'][$row . '-' . $col] = array(
            '#type' => 'textfield',
            '#default_value' => isset($default_values[$row][$col]) ? $default_values[$row][$col] : '',
            '#size' => $size,
          );
        }
      }
      break;
    case 'matrix_custom':
      $settings = unserialize($field['settings']['settings']);
      $define = $field['settings']['define'];
      if ($rows_count == -1) {
        if (count($default_values) > 3) {

          //check to see how big the grid should be  based on previously submitted data
          $rows_count = count($default_values);
        }
        else {
          $rows_count = 3;
        }
        if (isset($form_state['storage'][$field_name]['rows'])) {
          $rows_count += $form_state['storage'][$field_name]['rows'];
        }
        $element['#matrix_rows'] = $rows_count;
        $element['#matrix_more_rows'] = TRUE;
        $row_labels = array();
      }
      else {
        $element['#matrix_rows'] = $rows_count;
        $element['#matrix_more_rows'] = FALSE;
        for ($row = 1; $row <= $rows_count; $row++) {
          $row_labels[$row] = isset($settings['rows'][$row]['title']) ? $settings['rows'][$row]['title'] : '';
        }
      }
      if ($cols_count == -1) {
        if (isset($default_values[1]) && count($default_values[1]) > 3) {

          //check to see how big the grid should be  based on previously submitted data
          $cols_count = count($default_values[1]);
        }
        else {
          $cols_count = 3;
        }
        if (isset($form_state['storage'][$field_name]['cols'])) {
          $cols_count += $form_state['storage'][$field_name]['cols'];
        }
        $element['#matrix_cols'] = $cols_count;
        $element['#matrix_more_cols'] = TRUE;
        $column_labels = array_fill(0, $cols_count, '&nbsp;');
      }
      else {
        $element['#matrix_cols'] = $cols_count;
        $element['#matrix_more_cols'] = FALSE;
        for ($col = 1; $col <= $cols_count; $col++) {
          $column_labels[$col] = isset($settings['cols'][$col]['title']) ? $settings['cols'][$col]['title'] : '';
        }
      }
      $js = '';
      for ($row = 1; $row <= $rows_count; $row++) {
        for ($col = 1; $col <= $cols_count; $col++) {

          //determine which field type should be shown for this cell

          //if define is cols then if a row is set to calculation or custom other than 'none' then it takes precidence (and visa versa)
          if ($define == 'cols') {
            if (isset($settings['rows'][$row]['field_type']) && in_array($settings['rows'][$row]['field_type'], array(
              'calculation',
              'custom',
            ))) {
              $field_settings = isset($settings['rows'][$row]) ? $settings['rows'][$row] : array(
                'field_type' => 'none',
              );
              $calculation_class = 'matrix-calc-col-' . $col;
            }
            else {
              $field_settings = isset($settings['cols'][$col]) ? $settings['cols'][$col] : array(
                'field_type' => 'none',
              );
              $calculation_class = 'matrix-calc-row-' . $row;
            }
          }
          elseif ($define == 'rows') {
            if (isset($settings['cols'][$col]['field_type']) && in_array($settings['cols'][$col]['field_type'], array(
              'calculation',
              'custom',
            ))) {
              $field_settings = isset($settings['cols'][$col]) ? $settings['cols'][$col] : array(
                'field_type' => 'none',
              );
              $calculation_class = 'matrix-calc-row-' . $row;
            }
            else {
              $field_settings = isset($settings['rows'][$row]) ? $settings['rows'][$row] : array(
                'field_type' => 'none',
              );
              $calculation_class = 'matrix-calc-col-' . $col;
            }
          }

          //now render the element
          $field_type = $field_settings['field_type'];
          if (isset($default_values[$row][$col])) {
            $default_value = $default_values[$row][$col];
          }
          else {
            $default_value[0] = '';
          }
          if (isset($field_settings[$field_type]['required']) && $field_settings[$field_type]['required'] == TRUE) {
            $required = TRUE;
            $required_marker = _theme('form_required_marker', array());
          }
          else {
            $required = FALSE;
            $required_marker = '';
          }
          switch ($field_type) {
            case 'none':
              $element['grid'][$row . '-' . $col] = array(
                '#matrix_row' => $row,
                '#matrix_column' => $col,
                '#field_name' => $element['#title'],
                '#type' => 'markup',
                '#markup' => $default_value[0],
                '#attributes' => array(
                  'class' => array(
                    'matrix-col-' . $col,
                    'matrix-row-' . $row,
                    'matrix-cell-' . $row . '-' . $col,
                  ),
                ),
              );
              break;
            case 'textfield':
              $element['grid'][$row . '-' . $col] = array(
                '#type' => 'textfield',
                '#matrix_row' => $row,
                '#matrix_column' => $col,
                '#field_name' => $element['#title'],
                '#field_prefix' => $field_settings['textfield']['prefix'],
                '#field_suffix' => $field_settings['textfield']['suffix'] . $required_marker,
                '#default_value' => $default_value[0],
                '#size' => $field_settings['textfield']['size'],
                '#matrix_required' => $required,
                '#element_validate' => array(
                  'matrix_required_validate',
                ),
                '#attributes' => array(
                  'class' => array(
                    'matrix-calc-col-' . $col,
                    'matrix-calc-row-' . $row,
                    'matrix-col-' . $col,
                    'matrix-row-' . $row,
                    'matrix-cell-' . $row . '-' . $col,
                  ),
                ),
              );
              break;
            case 'select':
              $element['grid'][$row . '-' . $col] = array(
                '#type' => 'select',
                '#matrix_row' => $row,
                '#matrix_column' => $col,
                '#field_name' => $element['#title'],
                '#default_value' => $default_value[0],
                '#matrix_required' => $required,
                '#element_validate' => array(
                  'matrix_required_validate',
                ),
                '#field_suffix' => $required_marker,
                '#options' => matrix_allowed_values($field, $field_settings, 'select'),
                '#attributes' => array(
                  'class' => array(
                    'matrix-calc-col-' . $col,
                    'matrix-calc-row-' . $row,
                    'matrix-col-' . $col,
                    'matrix-row-' . $row,
                    'matrix-cell-' . $row . '-' . $col,
                  ),
                ),
              );
              break;
            case 'radios':
              $element['grid'][$row . '-' . $col] = array(
                '#type' => 'radios',
                '#matrix_row' => $row,
                '#matrix_column' => $col,
                '#field_name' => $element['#title'],
                '#default_value' => $default_value[0],
                '#matrix_required' => $required,
                '#element_validate' => array(
                  'matrix_required_validate',
                ),
                '#prefix' => $required_marker,
                '#options' => matrix_allowed_values($field, $field_settings, 'radios'),
                '#attributes' => array(
                  'class' => array(
                    'matrix-calc-col-' . $col,
                    'matrix-calc-row-' . $row,
                    'matrix-col-' . $col,
                    'matrix-row-' . $row,
                    'matrix-cell-' . $row . '-' . $col,
                  ),
                ),
              );
              break;
            case 'checkboxes':
              $element['grid'][$row . '-' . $col] = array(
                '#type' => 'checkboxes',
                '#matrix_row' => $row,
                '#matrix_column' => $col,
                '#field_name' => $element['#title'],
                '#default_value' => $default_value,
                '#matrix_required' => $required,
                '#element_validate' => array(
                  'matrix_required_validate',
                ),
                '#prefix' => $required_marker,
                '#options' => matrix_allowed_values($field, $field_settings, 'checkboxes'),
                '#attributes' => array(
                  'class' => array(
                    'matrix-calc-col-' . $col,
                    'matrix-calc-row-' . $row,
                    'matrix-col-' . $col,
                    'matrix-row-' . $row,
                    'matrix-cell-' . $row . '-' . $col,
                  ),
                ),
              );
              break;
            case 'calculation':
              $element['grid'][$row . '-' . $col] = array(
                '#type' => 'hidden',
                '#matrix_row' => $row,
                '#matrix_column' => $col,
                '#field_name' => $element['#title'],
                '#suffix' => '<div id="matrix-result-' . $field_name . '-' . $row . '-' . $col . '">&nbsp;</div>',
                '#attributes' => array(
                  'class' => array(
                    'matrix-col-' . $col,
                    'matrix-row-' . $row,
                    'matrix-cell-' . $row . '-' . $col,
                  ),
                ),
              );
              $opperation = $field_settings['calculation']['calculation'];
              $prefix = $field_settings['calculation']['prefix'];
              $suffix = $field_settings['calculation']['suffix'];
              $rounding = $field_settings['calculation']['rounding'];
              $js .= "jQuery('.{$calculation_class}').change(function(){window.matrix.{$opperation}('{$calculation_class}', '{$row}-{$col}', '{$field_name}', '{$prefix}', '{$suffix}', '{$rounding}')});\n\n                      window.matrix.{$opperation}('{$calculation_class}', '{$row}-{$col}', '{$field_name}', '{$prefix}', '{$suffix}', '{$rounding}');\n";
              break;
            case 'custom':
              $callback = $field_settings['custom']['custom'];
              $element['grid'][$row . '-' . $col] = array(
                '#type' => 'hidden',
                '#matrix_row' => $row,
                '#matrix_column' => $col,
                '#suffix' => '<div id="matrix-result-' . $field_name . '-' . $row . '-' . $col . '">&nbsp;</div>',
                '#attributes' => array(
                  'class' => array(
                    'matrix-col-' . $col,
                    'matrix-row-' . $row,
                    'matrix-cell-' . $row . '-' . $col,
                  ),
                ),
              );
              $js .= "jQuery('.{$calculation_class}').change(function() {window.matrix.custom('{$callback}', '{$calculation_class}', '{$row}-{$col}', '{$field_name}')});\n\n                      window.matrix.custom('{$callback}', '{$calculation_class}', '{$row}-{$col}', '{$field_name}');\n";
              break;
          }
        }
      }
      break;
  }
  if (!empty($js)) {

    // @FIXME
    // The Assets API has totally changed. CSS, JavaScript, and libraries are now
    // attached directly to render arrays using the #attached property.
    //
    //
    // @see https://www.drupal.org/node/2169605
    // @see https://www.drupal.org/node/2408597
    // drupal_add_js(drupal_get_path('module', 'matrix') .'/matrix.js');
    // @FIXME
    // The Assets API has totally changed. CSS, JavaScript, and libraries are now
    // attached directly to render arrays using the #attached property.
    //
    //
    // @see https://www.drupal.org/node/2169605
    // @see https://www.drupal.org/node/2408597
    // drupal_add_js('jQuery(document).ready(function () {'. $js .'});', array('type' => 'inline', 'scope' => 'footer'));
  }
  $element['more_rows'] = array(
    '#type' => 'button',
    '#value' => t('More rows'),
    '#name' => 'matrix-button-' . $field_name,
    '#ajax' => array(
      'callback' => 'matrix_field_ajax_callback',
      'wrapper' => 'matrix-field-' . $field_name,
    ),
    '#limit_validation_errors' => array(),
  );
  $element['more_cols'] = array(
    '#type' => 'button',
    '#value' => t('More columns'),
    '#name' => 'matrix-button-' . $field_name,
    '#ajax' => array(
      'callback' => 'matrix_field_ajax_callback',
      'wrapper' => 'matrix-field-' . $field_name,
    ),
    '#limit_validation_errors' => array(),
  );
  $element['#theme'] = 'matrix_table';
  $element['#row_labels'] = $row_labels;
  $element['#column_labels'] = $column_labels;
  $element['#element_validate'] = array(
    'matrix_field_widget_validate',
  );
  $form_state['matrix'] = $element['#field_name'];

  //used to identify this element in the ajax callback.
  return $element;
}