diff --git a/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php index db6ebf6..f07b193 100644 --- a/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php +++ b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php @@ -5,6 +5,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\TypedData\TranslatableInterface; use Symfony\Component\Validator\ConstraintViolationInterface; /** @@ -25,7 +26,21 @@ class PathWidget extends WidgetBase { */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $entity = $items->getEntity(); + /** @var \Drupal\path\Plugin\Field\FieldType\PathItem $path */ $path = $items[$delta]; + $use_existing_path = !$path->isEmpty(); + if ($entity instanceof TranslatableInterface && !$path->isEmpty()) { + // Have to load the unchanged entity because content_translation will have + // already loaded the new translation. + // @see \Drupal\content_translation\Controller\ContentTranslationController::prepareTranslation(). + /** @var \Drupal\Core\TypedData\TranslatableInterface $unchanged_entity */ + $unchanged_entity = entity_load_unchanged($entity->getEntityTypeId(), $entity->id()); + if (!in_array($entity->language(), array_keys($unchanged_entity->getTranslationLanguages()))) { + // If this current language is not in translated versions do not use the + // existing path. + $use_existing_path = FALSE; + } + } $element += array( '#element_validate' => array(array(get_class($this), 'validateFormElement')), @@ -33,14 +48,15 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $element['alias'] = array( '#type' => 'textfield', '#title' => $element['#title'], - '#default_value' => !$path->isEmpty() ? $path->get('alias')->getValue() : '', + '#default_value' => $use_existing_path ? $path->get('alias')->getValue() : '', '#required' => $element['#required'], '#maxlength' => 255, '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "/about" when writing an about page.'), ); + $element['pid'] = array( '#type' => 'value', - '#value' => !$path->isEmpty() ? $path->get('pid')->getValue() : NULL, + '#value' => $use_existing_path ? $path->get('pid')->getValue() : NULL, ); $element['source'] = array( '#type' => 'value', @@ -48,7 +64,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ); $element['langcode'] = array( '#type' => 'value', - '#value' => !$path->isEmpty() ? $path->getLangcode() : $items->getLangcode(), + '#value' => $use_existing_path ? $path->getLangcode() : $items->getLangcode(), ); return $element; }