You are here

bricks_inline.module in Bricks​ 8

Same filename and directory in other branches
  1. 2.x modules/bricks_inline/bricks_inline.module

File

modules/bricks_inline/bricks_inline.module
View source
<?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_get_path('module', '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[] = 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' => '',
        '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'], $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;

    // +
  }
}

Functions

Namesort descending Description
bricks_inline_inline_entity_form_entity_table Overrides theme_inline_entity_form_entity_table() with a help from hook_theme_registry_alter().
bricks_inline_inline_entity_form_reference_form_alter Implements hook_inline_entity_form_reference_form_alter().
bricks_inline_preprocess_inline_entity_form_entity_table Prepares variables for theme_inline_entity_form_entity_table().
bricks_inline_theme_registry_alter Implements hook_theme_registry_alter().
_inline_entity_form_preprocess_inline_entity_form_entity_table Hack: copy-and-paste from theme_inline_entity_form_entity_table().