Set maximum columns/rows

Created on 16 October 2023, 12 months ago
Updated 26 October 2023, 11 months ago

Is there a way to set a maximum number of columns for a Tablefield?

I'm not seeing a clear way to alter the rebuild form or add a validator.

πŸ’¬ Support request
Status

Active

Version

2.4

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jeffschuler Boulder, Colorado

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jeffschuler
  • @jeffschuler You can code hook_element_info_alter and pass element validation there.

    /**
     * Implements hook_element_info_alter().
     */
    function module_name_element_info_alter(array &$info) {
      if (isset($info['tablefield'])) {
        $info['tablefield']['#element_validate'][] = 'module_name_tablefield_validate';
      }
    }
    
    /**
     * Form element validation handler for #type 'tablefield'.
     */
    function module_name_tablefield_validate(&$element, FormStateInterface $form_state, &$complete_form) {
      // TODO
    }
    
  • Status changed to Fixed 12 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jeffschuler Boulder, Colorado

    Thanks @ad0z! We'll give this a try and I'll re-open if we have issues.

  • Status changed to Active 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jeffschuler Boulder, Colorado

    Couldn't get hook_element_info_alter() to work, but was able to set a validator in hook_field_widget_WIDGET_TYPE_form_alter() (our tablefield is inside a paragraph) such that when the form is submitted, the number of columns are checked and a form error is thrown if above our max.

    However, I want to validate and set the error on the submission of the AJAX rebuild functionality itself ~ when the Rebuild button is clicked. (I don't want the user to be able to rebuild their table above max rows and then not figure it out until the submit the whole node.)

    Seems like this is because the $element['tablefield']['rebuild']['rebuild'] sub-form is built in the processTableField() function and doesn't yet exist when form alters happen.

    I tried setting an #after_rebuild hook on the form, but $element['tablefield']['rebuild'] still wasn't set, though $element['tablefield']['#rebuild'] was true...

  • >Couldn't get hook_element_info_alter() to work

    I was checking it before I recommended it and it's working or there is misunderstood between us.
    Here is fast example which is working for me:

    /**
     * Implements hook_element_info_alter().
     */
    function custom_module_element_info_alter(array &$info) {
      if (isset($info['tablefield'])) {
        $info['tablefield']['#element_validate'][] = 'custom_module_tablefield_validate';
      }
    }
    
    /**
     * Form element validation handler for #type 'tablefield'.
     */
    function custom_module_tablefield_validate(&$element, FormStateInterface $form_state, &$complete_form) {
      if ($form_state->getTriggeringElement()['#name'] === 'tablefield-rebuild-field_test_table-0') {
        $values = $form_state->getValues()['field_test_table'][0]['tablefield']['rebuild'];
        $cols = $values['cols'];
        $rows = $values['rows'];
        if ($rows > 10) {
          $form_state->setError($element['tablefield']['rebuild']['rows'], 'Rows error');
        }
        if ($cols > 10) {
          $form_state->setError($element['tablefield']['rebuild']['cols'], 'Cols error');
        }
      }
    }
    
  • πŸ‡ΊπŸ‡ΈUnited States jeffschuler Boulder, Colorado

    @ad0z: thank you.

    After some debugging, I see that the reason the validation callback wasn't being called is because of this change that I made in patch #9 on πŸ› If required, all cells need not be full Needs review :

    - if ($form_state->getTriggeringElement()) {
    - $element['#element_validate'][] = [$this, 'validateTablefield'];
    - }
    + $element['#element_validate'][] = [$this, 'validateTablefield'];

    Some more digging to do...

Production build 0.71.5 2024