template_preprocess_file_widget_multiple(&$variables)
Prepares variables for multi file form widget templates.
Default template: file-widget-multiple.html.twig.
Parameters
array $variables: An associative array containing:
- element: A render element representing the widgets.
File
- core/modules/file/file.field.inc, line 21
- Field module functionality for the File module.
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 97 98 99 100 101 102 103 104 105 106 107 108 | function template_preprocess_file_widget_multiple(& $variables ) { $element = $variables [ 'element' ]; // Special ID and classes for draggable tables. $weight_class = $element [ '#id' ] . '-weight' ; $table_id = $element [ '#id' ] . '-table' ; // Build up a table of applicable fields. $headers = array (); $headers [] = t( 'File information' ); if ( $element [ '#display_field' ]) { $headers [] = array ( 'data' => t( 'Display' ), 'class' => array ( 'checkbox' ), ); } $headers [] = t( 'Weight' ); $headers [] = t( 'Operations' ); // Get our list of widgets in order (needed when the form comes back after // preview or failed validation). $widgets = array (); foreach (Element::children( $element ) as $key ) { $widgets [] = & $element [ $key ]; } usort( $widgets , '_field_multiple_value_form_sort_helper' ); $rows = array (); foreach ( $widgets as $key => & $widget ) { // Save the uploading row for last. if ( empty ( $widget [ '#files' ])) { $widget [ '#title' ] = $element [ '#file_upload_title' ]; $widget [ '#description' ] = \Drupal::service( 'renderer' )->renderPlain( $element [ '#file_upload_description' ]); continue ; } // Delay rendering of the buttons, so that they can be rendered later in the // "operations" column. $operations_elements = array (); foreach (Element::children( $widget ) as $sub_key ) { if (isset( $widget [ $sub_key ][ '#type' ]) && $widget [ $sub_key ][ '#type' ] == 'submit' ) { hide( $widget [ $sub_key ]); $operations_elements [] = & $widget [ $sub_key ]; } } // Delay rendering of the "Display" option and the weight selector, so that // each can be rendered later in its own column. if ( $element [ '#display_field' ]) { hide( $widget [ 'display' ]); } hide( $widget [ '_weight' ]); // Render everything else together in a column, without the normal wrappers. $widget [ '#theme_wrappers' ] = array (); $information = drupal_render( $widget ); $display = '' ; if ( $element [ '#display_field' ]) { unset( $widget [ 'display' ][ '#title' ]); $display = array ( 'data' => render( $widget [ 'display' ]), 'class' => array ( 'checkbox' ), ); } $widget [ '_weight' ][ '#attributes' ][ 'class' ] = array ( $weight_class ); $weight = render( $widget [ '_weight' ]); // Arrange the row with all of the rendered columns. $row = array (); $row [] = $information ; if ( $element [ '#display_field' ]) { $row [] = $display ; } $row [] = $weight ; // Show the buttons that had previously been marked as hidden in this // preprocess function. We use show() to undo the earlier hide(). foreach (Element::children( $operations_elements ) as $key ) { show( $operations_elements [ $key ]); } $row [] = array ( 'data' => $operations_elements , ); $rows [] = array ( 'data' => $row , 'class' => isset( $widget [ '#attributes' ][ 'class' ]) ? array_merge ( $widget [ '#attributes' ][ 'class' ], array ( 'draggable' )) : array ( 'draggable' ), ); } $variables [ 'table' ] = array ( '#type' => 'table' , '#header' => $headers , '#rows' => $rows , '#attributes' => array ( 'id' => $table_id , ), '#tabledrag' => array ( array ( 'action' => 'order' , 'relationship' => 'sibling' , 'group' => $weight_class , ), ), '#access' => ! empty ( $rows ), ); $variables [ 'element' ] = $element ; } |
Please login to continue.