Created
July 22, 2016 10:48
-
-
Save szeidler/93dabe93c8e68d10d278d1f05872545f to your computer and use it in GitHub Desktop.
Add multi_edit support to inline_entity_form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/includes/commerce_product.inline_entity_form.inc b/includes/commerce_product.inline_entity_form.inc | |
index 5dcb6f2..62da0b6 100644 | |
--- a/includes/commerce_product.inline_entity_form.inc | |
+++ b/includes/commerce_product.inline_entity_form.inc | |
@@ -160,7 +160,7 @@ class CommerceProductInlineEntityFormController extends EntityInlineEntityFormCo | |
); | |
$entity_form['product_details'] = array( | |
'#type' => 'fieldset', | |
- '#title' => t('Details'), | |
+ '#title' => $this->getSetting('multi_edit') ? $product->title . ' (' . $product->sku . ')' : t('Details'), | |
'#attributes' => array('class' => array('ief-product-details', 'ief-entity-fieldset')), | |
); | |
diff --git a/includes/entity.inline_entity_form.inc b/includes/entity.inline_entity_form.inc | |
index 89a4246..5621f21 100644 | |
--- a/includes/entity.inline_entity_form.inc | |
+++ b/includes/entity.inline_entity_form.inc | |
@@ -146,6 +146,7 @@ class EntityInlineEntityFormController { | |
$defaults['match_operator'] = 'CONTAINS'; | |
$defaults['allow_clone'] = FALSE; | |
$defaults['delete_references'] = FALSE; | |
+ $defaults['multi_edit'] = FALSE; | |
$labels = $this->defaultLabels(); | |
$defaults['override_labels'] = FALSE; | |
$defaults['label_singular'] = $labels['singular']; | |
@@ -211,6 +212,12 @@ class EntityInlineEntityFormController { | |
'#title' => t('Delete referenced @label when the parent entity is deleted.', array('@label' => $labels['plural'])), | |
'#default_value' => $this->settings['delete_references'], | |
); | |
+ $form['multi_edit'] = array( | |
+ '#type' => 'checkbox', | |
+ '#title' => t('Multi-edit?'), | |
+ '#default_value' => $this->settings['multi_edit'], | |
+ '#description' => t('Automatically show the edit forms for all objects.'), | |
+ ); | |
$form['override_labels'] = array( | |
'#type' => 'checkbox', | |
'#title' => t('Override labels'), | |
diff --git a/inline_entity_form.module b/inline_entity_form.module | |
index c1a7d82..aa49920 100644 | |
--- a/inline_entity_form.module | |
+++ b/inline_entity_form.module | |
@@ -539,20 +539,30 @@ function inline_entity_form_field_widget_form(&$form, &$form_state, $field, $ins | |
'#cardinality' => (int) $cardinality, | |
); | |
- // Get the fields that should be displayed in the table. | |
- $fields = $controller->tableFields($settings['bundles']); | |
- $context = array( | |
- 'parent_entity_type' => $instance['entity_type'], | |
- 'parent_bundle' => $instance['bundle'], | |
- 'field_name' => $instance['field_name'], | |
- 'entity_type' => $settings['entity_type'], | |
- 'allowed_bundles' => $settings['bundles'], | |
- ); | |
- drupal_alter('inline_entity_form_table_fields', $fields, $context); | |
+ if ($controller->getSetting('multi_edit')) { | |
+ $fields = array(); | |
+ } | |
+ else { | |
+ // Get the fields that should be displayed in the table. | |
+ $fields = $controller->tableFields($settings['bundles']); | |
+ $context = array( | |
+ 'parent_entity_type' => $instance['entity_type'], | |
+ 'parent_bundle' => $instance['bundle'], | |
+ 'field_name' => $instance['field_name'], | |
+ 'entity_type' => $settings['entity_type'], | |
+ 'allowed_bundles' => $settings['bundles'], | |
+ ); | |
+ drupal_alter('inline_entity_form_table_fields', $fields, $context); | |
+ } | |
$element['entities']['#table_fields'] = $fields; | |
$weight_delta = max(ceil(count($form_state['inline_entity_form'][$ief_id]['entities']) * 1.2), 50); | |
foreach ($form_state['inline_entity_form'][$ief_id]['entities'] as $key => $value) { | |
+ // Optionally auto-load the edit forms. | |
+ if ($controller->getSetting('multi_edit') && (!isset($value['form']) || $value['form'] != 'remove')) { | |
+ $value['form'] = 'edit'; | |
+ } | |
+ | |
// Data used by theme_inline_entity_form_entity_table(). | |
$element['entities'][$key]['#entity'] = $entity = $value['entity']; | |
$element['entities'][$key]['#needs_save'] = $value['needs_save']; | |
@@ -563,8 +573,10 @@ function inline_entity_form_field_widget_form(&$form, &$form_state, $field, $ins | |
// First check to see if this entity should be displayed as a form. | |
if (!empty($value['form'])) { | |
$element['entities'][$key]['delta'] = array( | |
- '#type' => 'value', | |
- '#value' => $value['weight'], | |
+ '#type' => 'weight', | |
+ '#delta' => 50, | |
+ '#default_value' => $value['weight'], | |
+ '#attributes' => array('class' => array('ief-entity-delta')), | |
); | |
$element['entities'][$key]['form'] = array( | |
'#type' => 'container', | |
@@ -586,9 +598,45 @@ function inline_entity_form_field_widget_form(&$form, &$form_state, $field, $ins | |
// Prepare data for the form callbacks. | |
$form = &$element['entities'][$key]['form']; | |
+ $row['delta'] = array( | |
+ '#type' => 'weight', | |
+ '#delta' => 50, | |
+ '#default_value' => $value['weight'], | |
+ '#attributes' => array('class' => array('ief-entity-delta')), | |
+ ); | |
+ | |
// Add the appropriate form. | |
if ($value['form'] == 'edit') { | |
$form += inline_entity_form_entity_form($controller, $form, $form_state); | |
+ | |
+ // When the multiedit option is enabled, replace the 'cancel' button | |
+ // with a 'remove' button. | |
+ if ($controller->getSetting('multi_edit')) { | |
+ unset($form['actions']['ief_' . $form['#op'] . '_cancel']); | |
+ unset($form['actions']['ief_' . $form['#op'] . '_save']); | |
+ $remove_form = inline_entity_form_remove_form($controller, $form, $form_state); | |
+ // If 'allow_existing' is on, the default removal operation is unlink | |
+ // and the access check for deleting happens inside the controller | |
+ // removeForm() method. | |
+ if (empty($entity_id) || entity_access('delete', $controller->entityType(), $entity)) { | |
+ $form['actions']['ief_entity_remove'] = array( | |
+ '#type' => 'submit', | |
+ '#value' => t('Remove'), | |
+ '#name' => 'ief-' . $ief_id . '-entity-remove-' . $key, | |
+ '#limit_validation_errors' => array(), | |
+ '#ajax' => array( | |
+ 'callback' => 'inline_entity_form_get_element', | |
+ 'wrapper' => $wrapper, | |
+ ), | |
+ '#submit' => array('inline_entity_form_open_row_form'), | |
+ '#ief_row_delta' => $key, | |
+ '#ief_row_form' => 'remove', | |
+ ); | |
+ } | |
+ | |
+ // Allow the button group to be themed. | |
+ $form['actions']['#attributes']['class'][] = 'form-actions'; | |
+ } | |
} | |
elseif ($value['form'] == 'remove') { | |
$form += inline_entity_form_remove_form($controller, $form, $form_state); | |
@@ -1680,7 +1728,13 @@ function theme_inline_entity_form_entity_table($variables) { | |
} | |
$header[] = $column; | |
} | |
- $header[] = t('Operations'); | |
+ // No operations column for no fields (multi_edit). | |
+ if (!empty($fields)) { | |
+ $header[] = t('Operations'); | |
+ } | |
+ else { | |
+ $header[] = array('data' => 'Edit', 'class' => array('ief-edit-header')); | |
+ } | |
// Build an array of entity rows for the table. | |
$rows = array(); | |
@@ -1702,11 +1756,14 @@ function theme_inline_entity_form_entity_table($variables) { | |
$cells[] = drupal_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'; | |
} | |
+ // $sort_cells variable to be used for edit forms. | |
+ $sort_cells = $cells; | |
// Add fields that represent the entity. | |
$wrapper = entity_metadata_wrapper($entity_type, $entity); | |
@@ -1748,17 +1805,26 @@ function theme_inline_entity_form_entity_table($variables) { | |
$cells[] = array('data' => $data, 'class' => array('inline-entity-form-' . $entity_type . '-' . $field_name)); | |
} | |
// Add the buttons belonging to the "Operations" column. | |
- $cells[] = drupal_render($form[$key]['actions']); | |
- // Create the row. | |
- $rows[] = array('data' => $cells, 'class' => $row_classes); | |
+ // No operations column for no fields (multi_edit). | |
+ if (!empty($fields)) { | |
+ $cells[] = drupal_render($form[$key]['actions']); | |
+ } | |
- // If the current entity array specifies a form, output it in the next row. | |
+ // Output the form as a table cell. | |
if (!empty($form[$key]['form'])) { | |
- $row = array( | |
- array('data' => drupal_render($form[$key]['form']), 'colspan' => count($fields) + 1), | |
- ); | |
- $rows[] = array('data' => $row, 'class' => array('ief-row-form'), 'no_striping' => TRUE); | |
+ // Edit form width fields (no multi_edit). | |
+ if (!empty($fields)) { | |
+ // Use sort cells and replace the fields cell with the edit form. | |
+ $cells = $sort_cells; | |
+ $cells[] = array('data' => drupal_render($form[$key]['form']), 'colspan' => count($fields) + 1); | |
+ } | |
+ // Edit forms width multi_edit option. | |
+ else { | |
+ $cells[] = drupal_render($form[$key]['form']); | |
+ } | |
} | |
+ // Create the row. | |
+ $rows[] = array('data' => $cells, 'class' => $row_classes); | |
} | |
if (!empty($rows)) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment