You are here

public static function InlineEntityFormComplex::buildEntityFormActions in Inline Entity Form 8

Adds actions to the inline entity form.

Parameters

array $element: Form array structure.

File

src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php, line 691

Class

InlineEntityFormComplex
Complex inline widget.

Namespace

Drupal\inline_entity_form\Plugin\Field\FieldWidget

Code

public static function buildEntityFormActions($element) {

  // Build a delta suffix that's appended to button #name keys for uniqueness.
  $delta = $element['#ief_id'];
  if ($element['#op'] == 'add') {
    $save_label = t('Create @type_singular', [
      '@type_singular' => $element['#ief_labels']['singular'],
    ]);
  }
  elseif ($element['#op'] == 'duplicate') {
    $save_label = t('Duplicate @type_singular', [
      '@type_singular' => $element['#ief_labels']['singular'],
    ]);
  }
  else {
    $delta .= '-' . $element['#ief_row_delta'];
    $save_label = t('Update @type_singular', [
      '@type_singular' => $element['#ief_labels']['singular'],
    ]);
  }

  // Add action submit elements.
  $element['actions'] = [
    '#type' => 'container',
    '#weight' => 100,
  ];
  $element['actions']['ief_' . $element['#op'] . '_save'] = [
    '#type' => 'submit',
    '#value' => $save_label,
    '#name' => 'ief-' . $element['#op'] . '-submit-' . $delta,
    '#limit_validation_errors' => [
      $element['#parents'],
    ],
    '#attributes' => [
      'class' => [
        'ief-entity-submit',
      ],
    ],
    '#ajax' => [
      'callback' => 'inline_entity_form_get_element',
      'wrapper' => 'inline-entity-form-' . $element['#ief_id'],
    ],
  ];
  $element['actions']['ief_' . $element['#op'] . '_cancel'] = [
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#name' => 'ief-' . $element['#op'] . '-cancel-' . $delta,
    '#limit_validation_errors' => [],
    '#ajax' => [
      'callback' => 'inline_entity_form_get_element',
      'wrapper' => 'inline-entity-form-' . $element['#ief_id'],
    ],
  ];

  // Add submit handlers depending on operation.
  if ($element['#op'] == 'add') {
    static::addSubmitCallbacks($element['actions']['ief_add_save']);
    $element['actions']['ief_add_cancel']['#submit'] = [
      [
        get_called_class(),
        'closeChildForms',
      ],
      [
        get_called_class(),
        'closeForm',
      ],
      'inline_entity_form_cleanup_form_state',
    ];
  }
  else {
    $element['actions']['ief_' . $element['#op'] . '_save']['#ief_row_delta'] = $element['#ief_row_delta'];
    $element['actions']['ief_' . $element['#op'] . '_cancel']['#ief_row_delta'] = $element['#ief_row_delta'];
    static::addSubmitCallbacks($element['actions']['ief_' . $element['#op'] . '_save']);
    $element['actions']['ief_' . $element['#op'] . '_save']['#submit'][] = [
      get_called_class(),
      'submitCloseRow',
    ];
    $element['actions']['ief_' . $element['#op'] . '_cancel']['#submit'] = [
      [
        get_called_class(),
        'closeChildForms',
      ],
      [
        get_called_class(),
        'submitCloseRow',
      ],
      'inline_entity_form_cleanup_row_form_state',
    ];
  }
  return $element;
}