You are here

function template_preprocess_inline_entity_form_entity_table in Inline Entity Form 8

Prepares variables for inline_entity_form_entity_table form templates.

Default template: inline-entity-form-entity-table.html.twig.

Parameters

array $variables: An associative array containing:

  • form: A render element representing the form.

File

./inline_entity_form.module, line 331
Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (for example, order -> line items), where the child entities are never managed outside the…

Code

function template_preprocess_inline_entity_form_entity_table(array &$variables) {
  $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'],
      'class' => [
        'inline-entity-form-' . $entity_type . '-' . $field_name,
      ],
    ];

    // The first column gets a special class.
    if ($first) {
      $column['class'][] = 'ief-first-column-header';
      $first = FALSE;
    }
    $header[] = $column;
  }
  $header[] = t('Operations');

  // 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' => [
          '#plain_text' => '',
        ],
        '#wrapper_attributes' => [
          'class' => [
            'ief-tabledrag-handle',
          ],
        ],
      ];
      $cells[] = [
        'data' => $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) {
      if ($field['type'] == 'label') {
        $data = [
          '#markup' => $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);

        // Backward compatibility for callbacks that just provide a string not an array.
        if (!is_array($data)) {
          $data = [
            '#markup' => $data,
          ];
        }
      }
      else {
        $data = [
          '#markup' => t('N/A'),
        ];
      }
      $cells[$field_name] = array_merge($data, [
        '#wrapper_attributes' => [
          'class' => [
            'inline-entity-form-' . $entity_type . '-' . $field_name,
          ],
        ],
      ]);
    }

    // Add the buttons belonging to the "Operations" column, when entity is not
    // being displayed as a form.
    if (empty($form[$key]['form'])) {
      $cells['actions'] = $form[$key]['actions'];
    }

    // Create the row.
    $rows[] = $cells + [
      '#attributes' => [
        'class' => $row_classes,
      ],
    ];

    // If the current entity array specifies a form, output it in the next row.
    if (!empty($form[$key]['form'])) {
      $row = [];
      $row[] = $form[$key]['form'] + [
        '#wrapper_attributes' => [
          'colspan' => count($fields) + 1,
        ],
      ];
      $rows[] = $row + [
        '#attributes' => [
          'class' => [
            'ief-row-form',
          ],
          'no_striping' => TRUE,
        ],
      ];
    }
  }
  if (!empty($rows)) {
    $tabledrag = [];
    if ($has_tabledrag) {
      $tabledrag = [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'ief-entity-delta',
        ],
      ];
    }
    $variables['table'] = [
      '#type' => 'table',
      '#header' => $header,
      '#attributes' => [
        'id' => 'ief-entity-table-' . $form['#id'],
        'class' => [
          'ief-entity-table',
        ],
      ],
      '#tabledrag' => $tabledrag,
    ] + $rows;
  }
}