You are here

function _bricks_preprocess_tabledrag_form in Bricks​ 2.x

Same name and namespace in other branches
  1. 8 bricks.module \_bricks_preprocess_tabledrag_form()

Helper function for hook_preprocess_field_multiple_value_form().

3 calls to _bricks_preprocess_tabledrag_form()
bricks_dynamic_preprocess_field_multiple_value_form in modules/bricks_dynamic/bricks_dynamic.module
Prepares variables for `field-multiple-value-form.html.twig`.
bricks_inline_preprocess_inline_entity_form_entity_table in modules/bricks_inline/bricks_inline.module
Prepares variables for theme_inline_entity_form_entity_table().
bricks_preprocess_field_multiple_value_form in ./bricks.module
Prepares variables for `field-multiple-value-form.html.twig`.

File

./bricks.module, line 275

Code

function _bricks_preprocess_tabledrag_form(&$variables, $element_key, $widget, $order_class, $render_options = FALSE) {
  $element = $variables[$element_key];
  $operation_key = NULL;

  // @TODO: Replace by 'Nested bricks' widget setting.
  if (isset($element['#widget']) && $element['#widget'] == $widget || isset($element[0]['#widget']) && $element[0]['#widget'] == $widget) {

    // @TODO: Tmp hack for the proper indent width calculation.
    $variables['table']['#header'][0]['style'] = 'min-width: 150px';
    $variables['table']['#header'][] = [
      'data' => t('Depth'),
      'class' => [
        'bricks-depth-header',
      ],
    ];
    if ($render_options) {

      // Find Operations column
      $operation_key = array_filter($variables['table']['#header'], function ($item) {
        return isset($item['is_operation']);
      });
      if (!empty($operation_key) && is_array($operation_key)) {
        $operation_key = array_keys($operation_key);
        $operation_key = array_pop($operation_key);

        // Insert new options column before operations.
        array_splice($variables['table']['#header'], $operation_key, 0, [
          [
            'data' => t('Options'),
          ],
        ]);
      }
    }
    $row = 0;
    foreach (Element::children($element) as $i => $key) {
      if ($key !== 'add_more' && $key !== 'header_actions') {
        $depth = $element[$key]['depth']['#value'];
        $indentation = [];
        if ($depth > 0) {
          $indentation = [
            '#theme' => 'indentation',
            '#size' => $depth,
          ];
        }
        $drag_cell =& $variables['table']['#rows'][$row]['data'][0];
        $drag_cell['data'] = !empty($indentation) ? \Drupal::service('renderer')
          ->render($indentation) : '' . $drag_cell['data'];

        // @TODO
        $drag_cell['style'] = 'width: auto; min-width: 150px';
        show($element[$key]['depth']);
        $variables['table']['#rows'][$row]['data'][] = \Drupal::service('renderer')
          ->render($element[$key]['depth']);
        if ($render_options && !is_null($operation_key)) {

          // Insert data row in options column.
          array_splice($variables['table']['#rows'][$row]['data'], $operation_key, 0, [
            [
              'data' => \Drupal::service('renderer')
                ->render($element[$key]['options']),
              'class' => 'inline-entity-form-brick-options',
            ],
          ]);
        }
      }
      if ($key !== 'add_more') {
        $row++;
      }
    }
    $tabledrag_options =& $variables['table']['#tabledrag'];
    $tabledrag_options[0]['relationship'] = 'all';
    $tabledrag_options[] = [
      'action' => 'depth',
      'relationship' => 'group',
      'group' => 'bricks-depth',
    ];

    // Fake option to enable indentation:
    $tabledrag_options[] = [
      'action' => 'match',
      'relationship' => 'parent',
      'group' => $order_class,
    ];
    $variables['table']['#attached']['library'][] = 'bricks/tabledrag.relationship-all';
  }
}