View source
<?php
namespace Drupal\entityconnect\Element;
use Drupal\Core\Url;
use Drupal\entityconnect\EntityconnectNestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Submit;
use Drupal\field\Entity\FieldStorageConfig;
class EntityconnectSubmit extends Submit {
public function getInfo() {
$class = get_class($this);
return [
'#key' => '',
'#field' => '',
'#entity_type_target' => 'node',
'#acceptable_types' => [],
'#add_child' => FALSE,
'#validate' => [
[
$class,
'validateSubmit',
],
],
'#submit' => [
[
$class,
'addEditButtonSubmit',
],
],
'#weight' => 1,
'#limit_validation_errors' => [],
] + parent::getInfo();
}
public static function preRenderButton($element) {
$element = parent::preRenderButton($element);
$element['#attached']['library'][] = 'entityconnect/entityconnect';
$element['#attributes']['class'][] = 'cancel';
if (empty($element['#attributes']['title'])) {
$element['#attributes']['title'] = $element['#add_child'] ? t('Add') : t('Edit');
}
return $element;
}
public static function validateSubmit(array $form, FormStateInterface $form_state) {
}
public static function addEditButtonSubmit(array $form, FormStateInterface $form_state) {
$cacheId = "entityconnect-" . $form['#build_id'];
$triggeringElement = $form_state
->getTriggeringElement();
$field = $triggeringElement['#field'];
$key = $triggeringElement['#key'];
$entityType = $triggeringElement['#entity_type_target'];
$acceptableTypes = isset($triggeringElement['#acceptable_types']) ? $triggeringElement['#acceptable_types'] : NULL;
$source_entity = $form_state
->getFormObject()
->getEntity();
$fieldInfo = FieldStorageConfig::loadByName($source_entity
->getEntityTypeId(), $field);
$parents = isset($triggeringElement['#parents']) ? $triggeringElement['#parents'] : NULL;
$keyExists = NULL;
$fieldContainer = EntityconnectNestedArray::getValue($form_state
->getUserInput(), $parents, $keyExists);
$target_id = [];
if ($keyExists) {
if (is_array($fieldContainer)) {
foreach ($fieldContainer as $key1 => $value) {
if (is_array($value)) {
foreach ($value as $key2 => $value2) {
if (!is_null($value2)) {
$target_id[$key2] = $value2;
}
}
}
elseif (is_numeric($key1)) {
$target_id[] = $value;
}
elseif ($key1 === 'target_id') {
$target_id = $value;
break;
}
}
}
else {
$target_id = $fieldContainer;
}
}
if (is_string($target_id) && !is_numeric($target_id)) {
$target_id = explode(',', $target_id);
}
if (is_array($target_id)) {
$target_id = array_filter($target_id);
if (count($target_id) == 1) {
$target_id = array_shift($target_id);
}
}
if (!$triggeringElement['#add_child'] && (!$target_id || $target_id == '_none')) {
\Drupal::messenger()
->addError(t('You must select at least one entity to update.'));
$form_state
->setRedirectUrl(Url::fromRoute('<current>'));
return;
}
$current_route = \Drupal::routeMatch();
$data = [
'form' => $form,
'form_state' => $form_state,
'dest_route_name' => $current_route
->getRouteName(),
'dest_route_params' => $current_route
->getRawParameters()
->all(),
'params' => \Drupal::request()->query
->all(),
'field' => $field,
'field_info' => $fieldInfo,
'key' => $key,
'add_child' => $triggeringElement['#add_child'],
'target_id' => $target_id,
'target_entity_type' => $entityType,
'acceptable_types' => $acceptableTypes,
'field_container' => $fieldContainer,
'field_container_key_exists' => $keyExists,
];
\Drupal::moduleHandler()
->alter('entityconnect_add_edit_button_submit', $data);
$tempStore = \Drupal::getContainer()
->get('entityconnect.cache');
$tempStore
->set($cacheId, $data);
\Drupal::request()->query
->remove('destination');
if ($data['add_child']) {
$form_state
->setRedirect('entityconnect.add', [
'cache_id' => $cacheId,
]);
}
else {
if ($data['target_id']) {
$form_state
->setRedirect('entityconnect.edit', [
'cache_id' => $cacheId,
]);
}
}
}
}