View source
<?php
use Drupal\Core\Render\Element;
function bricks_inline_theme_registry_alter(&$theme_registry) {
$theme_registry['inline_entity_form_entity_table']['theme path'] = drupal_get_path('module', 'bricks_inline');
$theme_registry['inline_entity_form_entity_table']['function'] = 'bricks_inline_inline_entity_form_entity_table';
}
function bricks_inline_inline_entity_form_reference_form_alter(&$reference_form, &$form_state) {
$reference_form['#element_validate'] = array_diff($reference_form['#element_validate'], [
'inline_entity_form_reference_form_validate',
]);
}
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);
}
}
function bricks_inline_inline_entity_form_entity_table($variables) {
if (!empty($variables['table'])) {
return \Drupal::service('renderer')
->render($variables['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);
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',
],
];
}
$first = TRUE;
foreach ($fields as $field_name => $field) {
$column = [
'data' => $field['label'],
];
if ($first) {
$column['class'] = [
'ief-first-column-header',
];
$first = FALSE;
}
$header[] = $column;
}
$header[] = [
'data' => t('Operations'),
'is_operation' => TRUE,
];
$rows = [];
foreach (Element::children($form) as $key) {
$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';
}
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'], $arguments);
}
$cells[] = [
'data' => $data,
'class' => [
'inline-entity-form-' . $entity_type . '-' . $field_name,
],
];
}
$cells[] = $renderer
->render($form[$key]['actions']);
$rows[] = [
'data' => $cells,
'class' => $row_classes,
];
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,
];
$variables['table'] = $table;
}
}