You are here

weight.module in Weight 8.3

Defines weight field types.

File

weight.module
View source
<?php

/**
 * @file
 * Defines weight field types.
 */

/**
 * Implements hook_preprocess_views_view_table().
 */
function weight_preprocess_views_view_table(&$variables) {

  // Check if view return results
  if (!$variables['result']) {
    return;
  }

  // Check for a weight selector field.
  foreach ($variables['view']->field as $field_key => $field) {
    if (isset($field->options['plugin_id']) && $field->options['plugin_id'] == 'weight_selector') {

      // Check if the weight selector is on the first column.
      $is_first_column = array_search($field_key, array_keys($variables['view']->field)) > 0 ? FALSE : TRUE;

      // Add the tabledrag attributes.
      foreach ($variables['rows'] as $key => $row) {
        if ($is_first_column) {

          // If the weight selector is the first column move it to the last
          // column, in order to make the draggable widget appear.
          $weight_selector = $variables['rows'][$key]['columns'][$field->field];
          unset($variables['rows'][$key]['columns'][$field->field]);
          $variables['rows'][$key]['columns'][$field->field] = $weight_selector;
          $header_weight_selector = $variables['header'][$field->field];
          unset($variables['header'][$field->field]);
          $variables['header'][$field->field] = $header_weight_selector;
        }

        // Add draggable attribute.
        $variables['rows'][$key]['attributes']
          ->addClass('draggable');
      }

      // The row key identify in an unique way a view grouped by a field.
      // Without row number, all the groups will share the same table_id
      // and just the first table can be draggable.
      $table_id = 'weight-table-' . $variables['view']->dom_id . '-row-' . $key;
      $variables['attributes']['id'] = $table_id;
      $options = [
        'table_id' => $table_id,
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'weight-selector',
      ];
      drupal_attach_tabledrag($variables, $options);
    }
  }
}

Functions

Namesort descending Description
weight_preprocess_views_view_table Implements hook_preprocess_views_view_table().