public function DomainPathHelper::submitEntityForm in Domain Path 8
Submit handler for the domain paths element on the entity form.
Parameters
array $form: The form array.
\Drupal\Core\Form\FormStateInterface $form_state: The form state object.
File
- src/
DomainPathHelper.php, line 312
Class
Namespace
Drupal\domain_pathCode
public function submitEntityForm($form, FormStateInterface $form_state) {
// Setup Variables
$entity = $form_state
->getFormObject()
->getEntity();
$entity_system_path = '/' . $entity
->toUrl()
->getInternalPath();
// Get the saved alias
$default_alias = $this->aliasManager
->getAliasByPath($entity_system_path);
$properties = [
'source' => $entity_system_path,
'language' => $entity
->language()
->getId(),
];
$path_values = $form_state
->getValue('path');
$domain_path_values = $path_values[0]['domain_path'];
$domain_path_storage = $this->entityTypeManager
->getStorage('domain_path');
// Check domain access settings if they are on the form.
$domain_access = [];
if (!empty($form['field_domain_access']) && !empty($form_state
->getValue('field_domain_access'))) {
foreach ($form_state
->getValue('field_domain_access') as $item) {
$domain_access[$item['target_id']] = $item['target_id'];
}
}
// If domain access is on for this form, we check the "all affiliates"
// checkbox, otherwise we just assume it's available on all domains.
$domain_access_all = !empty($form['field_domain_all_affiliates']) ? $form_state
->getValue('field_domain_all_affiliates')['value'] : TRUE;
// If not set to delete, then save changes.
if (empty($domain_path_values['domain_path_delete'])) {
unset($domain_path_values['domain_path_delete']);
foreach ($domain_path_values as $domain_id => $domain_path_data) {
$alias = $domain_path_data['path'];
if ($this->moduleHandler
->moduleExists('domain_path_pathauto')) {
$domain_path_pathauto_service = \Drupal::service('domain_path_pathauto.generator');
if ($domain_path_data['pathauto']) {
// Generate alias using pathauto.
$alias = $domain_path_pathauto_service
->createEntityAlias($entity, 'return', $domain_id);
// Remember pathauto default enabled setting.
$domain_path_pathauto_service
->setDomainPathPathautoState($entity, $domain_id, TRUE);
}
else {
// Delete pathauto default enabled setting.
$domain_path_pathauto_service
->deleteDomainPathPathautoState($entity, $domain_id);
}
}
// Get the existing domain path for this domain if it exists.
$properties['domain_id'] = $domain_id;
$domain_paths = $domain_path_storage
->loadByProperties($properties);
$domain_path = $domain_paths ? reset($domain_paths) : NULL;
$domain_has_access = $domain_access_all || $domain_access && !empty($domain_access[$domain_id]);
// We don't want to save the alias if the domain path field is empty,
// or if it matches the default alias, or if the domain doesn't have
// access to this entity.
if (!$alias || $alias == $default_alias || !$domain_has_access) {
// Delete the existing domain path.
if ($domain_path) {
$domain_path
->delete();
}
continue;
}
// Create or update the domain path.
$properties_map = [
'alias' => $alias,
'domain_id' => $domain_id,
] + $properties;
if (!$domain_path) {
$domain_path = $domain_path_storage
->create([
'type' => 'domain_path',
]);
foreach ($properties_map as $field => $value) {
$domain_path
->set($field, $value);
}
$domain_path
->save();
}
else {
if ($domain_path
->get('alias')->value != $alias) {
$domain_path
->set('alias', $alias);
$domain_path
->save();
}
}
}
}
else {
// Delete all domain path aliases.
$domain_paths = $domain_path_storage
->loadByProperties($properties);
foreach ($domain_paths as $domain_path) {
$domain_path
->delete();
}
}
}