diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index a3f8892..78bbdda 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -34,20 +34,10 @@ class PathItem extends FieldItemBase { */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['alias'] = DataDefinition::create('string') - /** - * @todo If we set to computed then: - * 1. In order for the value to work with \Drupal\serialization\Normalizer\ComplexDataNormalizer::normalize - * We have to override getIterator as commented out below to set $include_computed to TRUE. - * 2. preSave() and postSave() methods in this class will not be called and therefore the you cannot - * create an alias with $entity->set('path',$values) or Node::create('path'=> $values) - */ - //->setComputed(TRUE) ->setLabel(t('Path alias')); $properties['pid'] = DataDefinition::create('integer') - //->setComputed(TRUE) ->setLabel(t('Path id')); $properties['langcode'] = DataDefinition::create('string') - //->setComputed(TRUE) ->setLabel(t('Language Code')); return $properties; } @@ -103,6 +93,15 @@ public function postSave($update) { } } else { + // The alias might have been auto-loaded for a different langcode before + // a translation was added. If the langcode property does not match the + // field langcode, then unset the path alias ID to store it as a new + // alias. + // @todo Revisit this in https://www.drupal.org/node/2511968 + if ($this->pid && $this->langcode != $this->getLangcode()) { + $this->pid = NULL; + } + // Delete old alias if user erased it. if ($this->pid && !$this->alias) { \Drupal::service('path.alias_storage')->delete(array('pid' => $this->pid)); @@ -148,13 +147,4 @@ protected function ensureLoaded() { } } - /** - * {@inheritdoc} - * @see propertyDefinitions() comment as to why this commented out. - */ - /*public function getIterator() { - return new \ArrayIterator($this->getProperties(TRUE)); - }*/ - - } diff --git a/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php index eb6a0c7..d33a7e3 100644 --- a/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php +++ b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php @@ -25,16 +25,6 @@ class PathWidget extends WidgetBase { */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $entity = $items->getEntity(); - $path = array(); - if (!$entity->isNew()) { - $path = $items->first()->getValue(); - } - $path += array( - 'pid' => NULL, - 'source' => !$entity->isNew() ? '/' . $entity->urlInfo()->getInternalPath() : NULL, - 'alias' => '', - 'langcode' => $items->getLangcode(), - ); $element += array( '#element_validate' => array(array(get_class($this), 'validateFormElement')), @@ -42,22 +32,22 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $element['alias'] = array( '#type' => 'textfield', '#title' => $element['#title'], - '#default_value' => $path['alias'], + '#default_value' => $items->alias, '#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['pid'], + '#value' => $items->pid, ); $element['source'] = array( '#type' => 'value', - '#value' => $path['source'], + '#value' => !$entity->isNew() ? '/' . $entity->toUrl()->getInternalPath() : NULL, ); $element['langcode'] = array( '#type' => 'value', - '#value' => $path['langcode'], + '#value' => $items->langcode, ); return $element; }