You are here

class Tablefield in TableField 8.2

Same name in this branch
  1. 8.2 src/Utility/Tablefield.php \Drupal\tablefield\Utility\Tablefield
  2. 8.2 src/Element/Tablefield.php \Drupal\tablefield\Element\Tablefield

Provides a form element for tabular data.

Plugin annotation

@FormElement("tablefield");

Hierarchy

Expanded class hierarchy of Tablefield

1 string reference to 'Tablefield'
tablefield.routing.yml in ./tablefield.routing.yml
tablefield.routing.yml
1 #type use of Tablefield
TablefieldWidget::formElement in src/Plugin/Field/FieldWidget/TablefieldWidget.php
Returns the form for a single field widget.

File

src/Element/Tablefield.php, line 15

Namespace

Drupal\tablefield\Element
View source
class Tablefield extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#cols' => 5,
      '#rows' => 5,
      '#lock' => FALSE,
      '#locked_cells' => [],
      '#input_type' => 'textfield',
      '#rebuild' => FALSE,
      '#import' => FALSE,
      '#process' => [
        [
          $class,
          'processTablefield',
        ],
      ],
      '#theme_wrappers' => [
        'form_element',
      ],
      '#addrow' => FALSE,
      '#add_row' => 0,
    ];
  }

  /**
   * Processes a checkboxes form element.
   */
  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;
  }

  /**
   * AJAX callback to rebuild the number of rows/columns.
   *
   * The basic idea is to descend down the list of #parent elements of the
   * triggering_element in order to locate the tablefield inside of the $form
   * array.
   *
   * That is the element that we need to return.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state object.
   */
  public static function ajaxCallbackRebuild(array $form, FormStateInterface $form_state) {
    $triggering_element = $form_state
      ->getTriggeringElement();

    // Go as deep as 'tablefield' key, but stop there (two more keys follow).
    $parents = array_slice($triggering_element['#array_parents'], 0, -2, TRUE);
    $rebuild = NestedArray::getValue($form, $parents);

    // We don't want to re-send the format/_weight options.
    unset($rebuild['format']);
    unset($rebuild['_weight']);

    // Set row value to default only if there is Add Row button clicked.
    $op = (string) $triggering_element['#value'];
    if ($op === 'Add Row') {
      $rebuild['rebuild']['rows']['#value'] = $rebuild['rebuild']['rows']['#default_value'];
    }
    return $rebuild;
  }

  /**
   * Submit handler.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state object.
   */
  public static function submitCallbackRebuild(array $form, FormStateInterface $form_state) {

    // Check what triggered this. We might need to rebuild or to import.
    $triggering_element = $form_state
      ->getTriggeringElement();
    $id = implode('-', array_slice($triggering_element['#parents'], 0, -3, TRUE));
    $parents = array_slice($triggering_element['#parents'], 0, -2, TRUE);
    $value = $form_state
      ->getValue($parents);
    if (isset($triggering_element['#name']) && $triggering_element['#name'] == 'tablefield-rebuild-' . $id || isset($triggering_element['#name']) && $triggering_element['#name'] == 'tablefield-addrow-' . $id) {
      $parents[] = 'rebuild';
      if (isset($triggering_element['#name']) && $triggering_element['#name'] == 'tablefield-addrow-' . $id) {
        $value['rebuild']['rows']++;
      }
      NestedArray::setValue($form_state
        ->getStorage(), $parents, $value['rebuild']);
      \Drupal::messenger()
        ->addStatus(t('Table structure rebuilt.'), FALSE);
    }
    elseif (isset($triggering_element['#name']) && $triggering_element['#name'] == 'tablefield-import-' . $id) {

      // Import CSV.
      $imported_tablefield = static::importCsv($id);
      if ($imported_tablefield) {
        $form_state
          ->setValue($parents, $imported_tablefield);
        $input = $form_state
          ->getUserInput();
        NestedArray::setValue($input, $parents, $imported_tablefield);
        $form_state
          ->setUserInput($input);
        $parents[] = 'rebuild';
        NestedArray::setValue($form_state
          ->getStorage(), $parents, $imported_tablefield['rebuild']);
      }
    }
    $form_state
      ->setRebuild();
  }

  /**
   * Helper function to import data from a CSV file.
   *
   * @param string $form_field_name
   *   Field name.
   *
   * @return mixed
   *   Table array or FALSE.
   */
  private static function importCsv($form_field_name) {
    $files = \Drupal::request()->files
      ->get('files');
    $file_upload = $files[$form_field_name];
    if (empty($file_upload)) {
      \Drupal::messenger()
        ->addError(t('Select a CSV file to upload.'));
      return FALSE;
    }
    if ($file_upload
      ->getClientOriginalExtension() != 'csv') {
      \Drupal::messenger()
        ->addError(t('Only files with the following extensions are allowed: %files-allowed.', [
        '%files-allowed' => 'csv',
      ]));
      return FALSE;
    }
    if (!empty($file_upload) && ($handle = fopen($file_upload
      ->getPathname(), 'r'))) {

      // Checking the encoding of the CSV file to be UTF-8.
      $encoding = 'UTF-8';
      if (function_exists('mb_detect_encoding')) {
        $file_contents = file_get_contents($file_upload
          ->getPathname());
        $encodings = [
          'UTF-8',
          'ISO-8859-1',
          'WINDOWS-1251',
        ];
        \Drupal::moduleHandler()
          ->alter('tablefield_encodings', $encodings);
        $encodings_list = implode(',', $encodings);
        $encoding = mb_detect_encoding($file_contents, $encodings_list);
      }

      // Populate CSV values.
      $tablefield = [];
      $max_cols = 0;
      $rows = 0;
      $separator = \Drupal::config('tablefield.settings')
        ->get('csv_separator');
      while (($csv = fgetcsv($handle, 0, $separator)) != FALSE) {
        foreach ($csv as $value) {
          $tablefield['table'][$rows][] = self::convertEncoding($value, $encoding);
        }
        $cols = count($csv);
        if ($cols > $max_cols) {
          $max_cols = $cols;
        }
        $rows++;
      }
      fclose($handle);
      $tablefield['rebuild']['cols'] = $max_cols;
      $tablefield['rebuild']['rows'] = $rows;
      \Drupal::messenger()
        ->addMessage(t('Successfully imported @file', [
        '@file' => $file_upload
          ->getClientOriginalName(),
      ]));
      return $tablefield;
    }
    \Drupal::messenger()
      ->addError(t('There was a problem importing @file.', [
      '@file' => $file_upload
        ->getClientOriginalName(),
    ]));
    return FALSE;
  }

  /**
   * Helper function to detect and convert strings not in UTF-8 to UTF-8.
   *
   * @param string $data
   *   The string which needs converting.
   * @param string $encoding
   *   The encoding of the CSV file.
   *
   * @return string
   *   UTF encoded string.
   */
  protected static function convertEncoding($data, $encoding) {

    // Converting UTF-8 to UTF-8 will not work.
    if ($encoding == 'UTF-8') {
      return $data;
    }

    // Try convert the data to UTF-8.
    if ($encoded_data = Unicode::convertToUtf8($data, $encoding)) {
      return $encoded_data;
    }

    // Fallback on the input data.
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
FormElement::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 15
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Tablefield::ajaxCallbackRebuild public static function AJAX callback to rebuild the number of rows/columns.
Tablefield::convertEncoding protected static function Helper function to detect and convert strings not in UTF-8 to UTF-8.
Tablefield::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
Tablefield::importCsv private static function Helper function to import data from a CSV file.
Tablefield::processTablefield public static function Processes a checkboxes form element.
Tablefield::submitCallbackRebuild public static function Submit handler.