You are here

public static function Tablefield::processTablefield in TableField 8.2

Processes a checkboxes form element.

File

src/Element/Tablefield.php, line 43

Class

Tablefield
Provides a form element for tabular data.

Namespace

Drupal\tablefield\Element

Code

public static function processTablefield(&$element, FormStateInterface $form_state, &$complete_form) {
  $parents = $element['#parents'];
  $value = is_array($element['#value']) ? $element['#value'] : [];

  // Check if the input_type is one of the allowed types.
  $input_type = in_array($element['#input_type'], [
    'textarea',
    'textfield',
  ]) ? $element['#input_type'] : 'textfield';

  // String to uniquely identify DOM elements.
  $id = implode('-', $element['#parents']);

  // This is being set in rebuild and import ajax calls.
  $storage = NestedArray::getValue($form_state
    ->getStorage(), $element['#parents']);

  // Fetch addrow value.
  if ($storage && isset($storage['tablefield']['rebuild'])) {
    $element['#cols'] = $storage['tablefield']['rebuild']['cols'];
    $element['#rows'] = $storage['tablefield']['rebuild']['rows'];
  }
  $element['#tree'] = TRUE;
  $element['tablefield'] = [
    '#type' => 'fieldset',
    '#attributes' => [
      'class' => [
        'form-tablefield',
      ],
    ],
    '#prefix' => '<div id="tablefield-' . $id . '-wrapper">',
    '#suffix' => '</div>',
  ];
  $element['tablefield']['table'] = [
    '#type' => 'table',
  ];

  // Assign value.
  $rows = isset($element['#rows']) ? $element['#rows'] : \Drupal::config('tablefield.settings')
    ->get('rows');
  $cols = isset($element['#cols']) ? $element['#cols'] : \Drupal::config('tablefield.settings')
    ->get('cols');
  for ($i = 0; $i < $rows; $i++) {
    for ($ii = 0; $ii < $cols; $ii++) {
      if (!empty($element['#locked_cells'][$i][$ii]) && !empty($element['#lock'])) {
        $element['tablefield']['table'][$i][$ii] = [
          '#type' => 'item',
          '#value' => $element['#locked_cells'][$i][$ii],
          '#title' => $element['#locked_cells'][$i][$ii],
        ];
      }
      else {
        $cell_value = isset($value[$i][$ii]) ? $value[$i][$ii] : '';
        $element['tablefield']['table'][$i][$ii] = [
          '#type' => $input_type,
          '#maxlength' => 2048,
          '#size' => 0,
          '#attributes' => [
            'class' => [
              'tablefield-row-' . $i,
              'tablefield-col-' . $ii,
            ],
            'style' => 'width:100%',
          ],
          '#default_value' => $cell_value,
        ];
      }
    }
  }

  // To change number of rows.
  if (!empty($element['#addrow'])) {
    $element['tablefield']['addrow']['row_value'] = [
      '#title' => t('How many rows'),
      '#type' => 'hidden',
      '#default_value' => $rows,
      '#value' => $rows,
    ];
    $element['tablefield']['addrow']['addrow'] = [
      '#type' => 'submit',
      '#value' => t('Add Row'),
      '#name' => 'tablefield-addrow-' . $id,
      '#attributes' => [
        'class' => [
          'tablefield-addrow',
        ],
      ],
      '#submit' => [
        [
          get_called_class(),
          'submitCallbackRebuild',
        ],
      ],
      '#limit_validation_errors' => [
        array_merge($parents, [
          'tablefield',
          'rebuild',
          'cols',
        ]),
        array_merge($parents, [
          'tablefield',
          'rebuild',
          'rows',
        ]),
        array_merge($parents, [
          'tablefield',
          'rebuild',
          'rebuild',
        ]),
      ],
      '#ajax' => [
        'callback' => 'Drupal\\tablefield\\Element\\Tablefield::ajaxCallbackRebuild',
        'progress' => [
          'type' => 'throbber',
          'message' => NULL,
        ],
        'wrapper' => 'tablefield-' . $id . '-wrapper',
        'effect' => 'fade',
      ],
    ];
  }

  // If no rebuild, we pass along the rows/cols as a value. Otherwise, we will
  // provide form elements to specify the size and ajax rebuild.
  if (empty($element['#rebuild'])) {
    $element['tablefield']['rebuild'] = [
      '#type' => 'value',
      'cols' => [
        '#type' => 'value',
        '#value' => $cols,
      ],
      'rows' => [
        '#type' => 'value',
        '#value' => $rows,
      ],
    ];
  }
  else {
    $element['tablefield']['rebuild'] = [
      '#type' => 'details',
      '#title' => t('Change number of rows/columns.'),
      '#open' => FALSE,
    ];
    $element['tablefield']['rebuild']['cols'] = [
      '#title' => t('How many Columns'),
      '#type' => 'number',
      '#size' => 5,
      '#default_value' => $cols,
      '#min' => 1,
    ];
    $element['tablefield']['rebuild']['rows'] = [
      '#title' => t('How many Rows'),
      '#type' => 'number',
      '#size' => 5,
      '#default_value' => $rows,
      '#min' => 1,
    ];
    $element['tablefield']['rebuild']['rebuild'] = [
      '#type' => 'submit',
      '#value' => t('Rebuild Table'),
      '#name' => 'tablefield-rebuild-' . $id,
      '#attributes' => [
        'class' => [
          'tablefield-rebuild',
        ],
      ],
      '#submit' => [
        [
          get_called_class(),
          'submitCallbackRebuild',
        ],
      ],
      '#limit_validation_errors' => [
        array_merge($parents, [
          'tablefield',
          'rebuild',
          'cols',
        ]),
        array_merge($parents, [
          'tablefield',
          'rebuild',
          'rows',
        ]),
        array_merge($parents, [
          'tablefield',
          'rebuild',
          'rebuild',
        ]),
      ],
      '#ajax' => [
        'callback' => 'Drupal\\tablefield\\Element\\Tablefield::ajaxCallbackRebuild',
        'progress' => [
          'type' => 'throbber',
          'message' => NULL,
        ],
        'wrapper' => 'tablefield-' . $id . '-wrapper',
        'effect' => 'fade',
      ],
    ];
  }

  // Allow import of a csv file.
  if (!empty($element['#import'])) {
    $element['tablefield']['import'] = [
      '#type' => 'details',
      '#title' => t('Import from CSV'),
      '#open' => FALSE,
    ];
    $element['tablefield']['import']['csv'] = [
      '#name' => 'files[' . $id . ']',
      '#title' => 'File upload',
      '#type' => 'file',
    ];
    $element['tablefield']['import']['import'] = [
      '#type' => 'submit',
      '#value' => t('Upload CSV'),
      '#name' => 'tablefield-import-' . $id,
      '#attributes' => [
        'class' => [
          'tablefield-rebuild',
        ],
      ],
      '#submit' => [
        [
          get_called_class(),
          'submitCallbackRebuild',
        ],
      ],
      '#limit_validation_errors' => [
        array_merge($parents, [
          'tablefield',
          'import',
          'csv',
        ]),
        array_merge($parents, [
          'tablefield',
          'import',
          'import',
        ]),
      ],
      '#ajax' => [
        'callback' => 'Drupal\\tablefield\\Element\\Tablefield::ajaxCallbackRebuild',
        'progress' => [
          'type' => 'throbber',
          'message' => NULL,
        ],
        'wrapper' => 'tablefield-' . $id . '-wrapper',
        'effect' => 'fade',
      ],
    ];
  }
  return $element;
}