bricks-2.x-dev/modules/bricks_inline/bricks_inline.module
modules/bricks_inline/bricks_inline.module
<?php /** * @file * {@inheritdoc} */ use Drupal\Core\Render\Element; /** * Implements hook_theme_registry_alter(). */ function bricks_inline_theme_registry_alter(&$theme_registry) { $theme_registry['inline_entity_form_entity_table']['theme path'] = \Drupal::service('extension.list.module')->getPath('bricks_inline'); $theme_registry['inline_entity_form_entity_table']['function'] = 'bricks_inline_inline_entity_form_entity_table'; } /** * Implements hook_inline_entity_form_reference_form_alter(). */ function bricks_inline_inline_entity_form_reference_form_alter(&$reference_form, &$form_state) { // Skip standard IEF validation to allow multiple references to // the same entity: $reference_form['#element_validate'] = array_diff($reference_form['#element_validate'], ['inline_entity_form_reference_form_validate']); } /** * Prepares variables for theme_inline_entity_form_entity_table(). */ function bricks_inline_preprocess_inline_entity_form_entity_table(&$variables) { _inline_entity_form_preprocess_inline_entity_form_entity_table($variables); if (!empty($variables['table']['#tabledrag'])) { _bricks_preprocess_tabledrag_form($variables, 'form', 'bricks_tree_inline', 'ief-entity-delta', TRUE); } } /** * Overrides theme_inline_entity_form_entity_table() * with a help from hook_theme_registry_alter(). */ function bricks_inline_inline_entity_form_entity_table($variables) { if (!empty($variables['table'])) { return \Drupal::service('renderer')->render($variables['table']); } } /** * Hack: copy-and-paste from theme_inline_entity_form_entity_table(). */ function _inline_entity_form_preprocess_inline_entity_form_entity_table(&$variables) { $renderer = \Drupal::service('renderer'); $form = $variables['form']; $entity_type = $form['#entity_type']; $fields = $form['#table_fields']; $has_tabledrag = \Drupal::entityTypeManager()->getHandler($entity_type, 'inline_form')->isTableDragEnabled($form); // Sort the fields by weight. uasort($fields, '\Drupal\Component\Utility\SortArray::sortByWeightElement'); $header = []; if ($has_tabledrag) { $header[] = ['data' => '', 'class' => ['ief-tabledrag-header']]; $header[] = ['data' => t('Sort order'), 'class' => ['ief-sort-order-header']]; } // Add header columns for each field. $first = TRUE; foreach ($fields as $field_name => $field) { $column = ['data' => $field['label']]; // The first column gets a special class. if ($first) { $column['class'] = ['ief-first-column-header']; $first = FALSE; } $header[] = $column; } $header[] = [ 'data' => t('Operations'), 'is_operation' => TRUE, ]; // Build an array of entity rows for the table. $rows = []; foreach (Element::children($form) as $key) { /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ $entity = $form[$key]['#entity']; $row_classes = ['ief-row-entity']; $cells = []; if ($has_tabledrag) { $cells[] = ['data' => '', 'class' => ['ief-tabledrag-handle']]; $cells[] = $renderer->render($form[$key]['delta']); $row_classes[] = 'draggable'; } // Add a special class to rows that have a form underneath, to allow // for additional styling. if (!empty($form[$key]['form'])) { $row_classes[] = 'ief-row-entity-form'; } foreach ($fields as $field_name => $field) { $data = ''; if ($field['type'] == 'label') { $data = $variables['form'][$key]['#label']; } elseif ($field['type'] == 'field' && $entity->hasField($field_name)) { $display_options = ['label' => 'hidden']; if (isset($field['display_options'])) { $display_options += $field['display_options']; } $data = $entity->get($field_name)->view($display_options); } elseif ($field['type'] == 'callback') { $arguments = [ 'entity' => $entity, 'variables' => $variables, ]; if (isset($field['callback_arguments'])) { $arguments = array_merge($arguments, $field['callback_arguments']); } $data = call_user_func_array($field['callback'], array_values($arguments)); } $cells[] = ['data' => $data, 'class' => ['inline-entity-form-' . $entity_type . '-' . $field_name]]; } // Add the buttons belonging to the "Operations" column. $cells[] = $renderer->render($form[$key]['actions']); // Create the row. $rows[] = ['data' => $cells, 'class' => $row_classes]; // If the current entity array specifies a form, output it in the next row. if (!empty($form[$key]['form'])) { $row = [ ['data' => $renderer->render($form[$key]['form']), 'colspan' => count($fields) + 1], ]; $rows[] = [ 'data' => $row, 'class' => ['ief-row-form'], 'no_striping' => TRUE, ]; } } if (!empty($rows)) { $tabledrag = []; if ($has_tabledrag) { $tabledrag = [ [ 'action' => 'order', 'relationship' => 'sibling', 'group' => 'ief-entity-delta', ], ]; } $table = [ '#type' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => [ 'id' => 'ief-entity-table-' . $form['#id'], 'class' => ['ief-entity-table'], ], '#tabledrag' => $tabledrag, ]; // return $renderer->render($table); // - $variables['table'] = $table; // + } }