template_preprocess_field_multiple_value_form(&$variables)
Prepares variables for individual form element templates.
Default template: field-multiple-value-form.html.twig.
Combines multiple values into a table with drag-n-drop reordering.
Parameters
array $variables: An associative array containing:
- element: A render element representing the form element.
File
- core/includes/theme.inc, line 1587
- The theme system, which controls the output of Drupal.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | function template_preprocess_field_multiple_value_form(& $variables ) { $element = $variables [ 'element' ]; $variables [ 'multiple' ] = $element [ '#cardinality_multiple' ]; if ( $variables [ 'multiple' ]) { $table_id = Html::getUniqueId( $element [ '#field_name' ] . '_values' ); $order_class = $element [ '#field_name' ] . '-delta-order' ; $header_attributes = new Attribute( array ( 'class' => array ( 'label' ))); if (! empty ( $element [ '#required' ])) { $header_attributes [ 'class' ][] = 'js-form-required' ; $header_attributes [ 'class' ][] = 'form-required' ; } $header = array ( array ( 'data' => array ( '#prefix' => '<h4' . $header_attributes . '>' , 'title' => array ( '#markup' => $element [ '#title' ], ), '#suffix' => '</h4>' , ), 'colspan' => 2, 'class' => array ( 'field-label' ), ), t( 'Order' , array (), array ( 'context' => 'Sort order' )), ); $rows = array (); // Sort items according to '_weight' (needed when the form comes back after // preview or failed validation). $items = array (); $variables [ 'button' ] = array (); foreach (Element::children( $element ) as $key ) { if ( $key === 'add_more' ) { $variables [ 'button' ] = & $element [ $key ]; } else { $items [] = & $element [ $key ]; } } usort( $items , '_field_multiple_value_form_sort_helper' ); // Add the items as table rows. foreach ( $items as $item ) { $item [ '_weight' ][ '#attributes' ][ 'class' ] = array ( $order_class ); // Remove weight form element from item render array so it can be rendered // in a separate table column. $delta_element = $item [ '_weight' ]; unset( $item [ '_weight' ]); $cells = array ( array ( 'data' => '' , 'class' => array ( 'field-multiple-drag' )), array ( 'data' => $item ), array ( 'data' => $delta_element , 'class' => array ( 'delta-order' )), ); $rows [] = array ( 'data' => $cells , 'class' => array ( 'draggable' ), ); } $variables [ 'table' ] = array ( '#type' => 'table' , '#header' => $header , '#rows' => $rows , '#attributes' => array ( 'id' => $table_id , 'class' => array ( 'field-multiple-table' ), ), '#tabledrag' => array ( array ( 'action' => 'order' , 'relationship' => 'sibling' , 'group' => $order_class , ), ), ); if (! empty ( $element [ '#description' ])) { $description_id = $element [ '#attributes' ][ 'aria-describedby' ]; $description_attributes [ 'id' ] = $description_id ; $variables [ 'description' ][ 'attributes' ] = new Attribute( $description_attributes ); $variables [ 'description' ][ 'content' ] = $element [ '#description' ]; // Add the description's id to the table aria attributes. $variables [ 'table' ][ '#attributes' ][ 'aria-describedby' ] = $element [ '#attributes' ][ 'aria-describedby' ]; } } else { $variables [ 'elements' ] = array (); foreach (Element::children( $element ) as $key ) { $variables [ 'elements' ][] = $element [ $key ]; } } } |
Please login to continue.