You are here

function _bricks_preprocess_tabledrag_form in Bricks​ 8

Same name and namespace in other branches
  1. 2.x 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 179

Code

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

  // @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',
      ],
    ];
    $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_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']);

        // @TODO: Get rid of $render_options hack.
        if ($render_options) {
          $element[$key]['options']['#prefix'] = $variables['table']['#rows'][$row]['data'][2]['data'];
          $variables['table']['#rows'][$row]['data'][2]['data'] = \Drupal::service('renderer')
            ->render($element[$key]['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';
  }
}