diff --git a/core/modules/path/lib/Drupal/path/Plugin/field/widget/NullWidget.php b/core/modules/path/lib/Drupal/path/Plugin/field/widget/NullWidget.php deleted file mode 100644 index 5f79f0e..0000000 --- a/core/modules/path/lib/Drupal/path/Plugin/field/widget/NullWidget.php +++ /dev/null @@ -1,106 +0,0 @@ -instance['settings']['pattern']['prefix'])) { - $prefix = $this->instance['settings']['pattern']['prefix'] . '/'; - } - else { - $prefix = ''; - } - $prefix_length = drupal_strlen($prefix); - - // @todo Consider to move this into path_field_load(). OTOH, that would - // (needlessly?) load URL aliases for every loaded entity. But then again, - // field data is cached, no? - $path = array(); - if (!$entity->isNew()) { - $uri = $entity->uri(); - $conditions = array( - 'source' => $uri['path'], - ); - if ($langcode != LANGUAGE_NOT_SPECIFIED) { - $conditions['langcode'] = $langcode; - } - if ($path = path_load($conditions)) { - // If there is an alias for this entity already, the configured prefix - // needs to be removed from it if it is identical, since the field is - // only supposed to store the actual user input. - if (drupal_substr($path['alias'], 0, $prefix_length) === $prefix) { - $path['value'] = drupal_substr($path['alias'], $prefix_length); - } - // If it is not, then the stored alias uses a different prefix than the - // one configured for this field instance, which can happen when - // manually saving an URL alias via Path module's administration pages, - // but also in case the field instance setting was not properly updated. - else { - $prefix = ''; - $path['value'] = $path['alias']; - } - } - else { - $path = $conditions; - } - } - $path += array( - 'pid' => NULL, - 'source' => NULL, - 'value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL, - 'langcode' => $langcode, - ); - - $element += array( - '#access' => FALSE, - '#tree' => TRUE, - ); - $element['prefix'] = array( - '#type' => 'value', - '#value' => $prefix, - ); - $element['value'] = array( - '#type' => 'value', - '#default_value' => $path['value'], - ); - $element['pid'] = array( - '#type' => 'value', - '#value' => $path['pid'], - ); - return $element; - } - -} diff --git a/core/modules/path/lib/Drupal/path/Plugin/field/widget/PathWidget.php b/core/modules/path/lib/Drupal/path/Plugin/field/widget/PathWidget.php index 71e7180..a6e73e0 100644 --- a/core/modules/path/lib/Drupal/path/Plugin/field/widget/PathWidget.php +++ b/core/modules/path/lib/Drupal/path/Plugin/field/widget/PathWidget.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\path\Plugin\field\widget\PathWidget. + * Contains Drupal\path\Plugin\field\widget\PathWidget. */ namespace Drupal\path\Plugin\field\widget; @@ -31,14 +31,6 @@ class PathWidget extends WidgetBase { public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { $entity = $element['#entity']; - if (isset($this->instance['settings']['pattern']['prefix'])) { - $prefix = $this->instance['settings']['pattern']['prefix'] . '/'; - } - else { - $prefix = ''; - } - $prefix_length = drupal_strlen($prefix); - // @todo Consider to move this into path_field_load(). OTOH, that would // (needlessly?) load URL aliases for every loaded entity. But then again, // field data is cached, no? @@ -52,20 +44,8 @@ public function formElement(array $items, $delta, array $element, $langcode, arr $conditions['langcode'] = $langcode; } if ($path = path_load($conditions)) { - // If there is an alias for this entity already, the configured prefix - // needs to be removed from it if it is identical, since the field is - // only supposed to store the actual user input. - if (drupal_substr($path['alias'], 0, $prefix_length) === $prefix) { - $path['value'] = drupal_substr($path['alias'], $prefix_length); - } - // If it is not, then the stored alias uses a different prefix than the - // one configured for this field instance, which can happen when - // manually saving an URL alias via Path module's administration pages, - // but also in case the field instance setting was not properly updated. - else { - $prefix = ''; - $path['value'] = $path['alias']; - } + // The field is supposed to store the actual user input. + $path['value'] = $path['alias']; } else { $path = $conditions; @@ -77,8 +57,7 @@ public function formElement(array $items, $delta, array $element, $langcode, arr 'value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL, 'langcode' => $langcode, ); - // @todo Entity-specific, dynamic token replacement for user-customizable - // part. Potentially use #ajax for this. + $language = language_load($langcode); $element += array( '#type' => 'container', @@ -96,20 +75,16 @@ public function formElement(array $items, $delta, array $element, $langcode, arr '#type' => 'value', '#value' => $path['pid'], ); - $element['prefix'] = array( - '#type' => 'value', - '#value' => $prefix, - ); $element['value'] = array( '#type' => 'textfield', // @todo Use the field instance label ($element['#title']) here or always // identical default for consistency? Are fields able to provide default // labels for instances somehow? '#title' => t('Permalink'), - '#field_prefix' => check_plain(url($prefix, array('absolute' => TRUE))), + '#field_prefix' => check_plain(url('', array('absolute' => TRUE, 'language' => $language))), '#default_value' => $path['value'], '#required' => $element['#required'], - '#maxlength' => 255 - drupal_strlen($prefix), + '#maxlength' => 255, ); $element['source'] = array( '#type' => 'value', @@ -130,6 +105,7 @@ public function validatePath(&$element, &$form_state, $form) { // Trim the submitted value. $element['value']['#value'] = trim($element['value']['#value']); form_set_value($element['value'], $element['value']['#value'], $form_state); + // Entity language needs special care. Since the language of the URL alias // depends on the entity language, and the entity language may be switched // right within the same form, we need to conditionally overload the @@ -142,7 +118,7 @@ public function validatePath(&$element, &$form_state, $form) { // Ensure that the submitted alias does not exist yet. $query = db_select('url_alias') - ->condition('alias', $element['prefix']['#value'] . $element['value']['#value']) + ->condition('alias', $element['value']['#value']) ->condition('langcode', $element['langcode']['#value']); if (!empty($element['source']['#value'])) { $query->condition('source', $element['source']['#value'], '<>'); diff --git a/core/modules/path/lib/Drupal/path/Tests/PathFieldCRUDTest.php b/core/modules/path/lib/Drupal/path/Tests/PathFieldCRUDTest.php index a7d5808..dc74d8e 100644 --- a/core/modules/path/lib/Drupal/path/Tests/PathFieldCRUDTest.php +++ b/core/modules/path/lib/Drupal/path/Tests/PathFieldCRUDTest.php @@ -44,7 +44,6 @@ function setUp() { // Create a path field for the node type. $this->field_name = drupal_strtolower($this->randomName()); $this->langcode = LANGUAGE_NOT_SPECIFIED; - $this->prefix = 'test/prefix'; $this->field = array( 'field_name' => $this->field_name, 'type' => 'path', @@ -54,12 +53,6 @@ function setUp() { 'field_name' => $this->field_name, 'entity_type' => 'node', 'bundle' => $this->nodeType->type, - 'settings' => array( - 'pattern' => array( - 'prefix' => $this->prefix, - 'auto' => FALSE, - ), - ), 'widget' => array( 'type' => 'path_default', ), @@ -87,10 +80,10 @@ function testBasicCRUD() { $data = $entity->{$this->field_name}[$this->langcode][0]; $this->assertTrue($data['pid']); $this->assertIdentical($data['value'], $edit['value']); - $this->assertIdentical($data['alias'], $this->prefix . '/' . $edit['value']); + $this->assertIdentical($data['alias'], $edit['value']); $path = path_load($data['pid']); $this->assertIdentical($path['source'], $uri['path']); - $this->assertIdentical($path['alias'], $this->prefix . '/' . $edit['value']); + $this->assertIdentical($path['alias'], $edit['value']); // Edit the field value and update the entity. $updated_value = 'updated-alias'; @@ -101,10 +94,10 @@ function testBasicCRUD() { $data = $entity->{$this->field_name}[$this->langcode][0]; $this->assertIdentical($data['pid'], $path['pid']); $this->assertIdentical($data['value'], $updated_value); - $this->assertIdentical($data['alias'], $this->prefix . '/' . $updated_value); + $this->assertIdentical($data['alias'], $updated_value); $path = path_load($data['pid']); $this->assertIdentical($path['source'], $uri['path']); - $this->assertIdentical($path['alias'], $this->prefix . '/' . $updated_value); + $this->assertIdentical($path['alias'], $updated_value); // Verify that there is only one alias. $count = db_query('SELECT COUNT(*) FROM {url_alias} WHERE source = :source', array( @@ -124,9 +117,6 @@ function testBasicCRUD() { $this->assertEqual($count, 0); } - function xtestCRUDWithChangedPrefix() { - } - /** * Tests updating of field data after deletion of aliases via URL alias API. */ diff --git a/core/modules/path/path.install b/core/modules/path/path.install index a1f206d..df0d312 100644 --- a/core/modules/path/path.install +++ b/core/modules/path/path.install @@ -17,10 +17,7 @@ function path_field_schema($field) { ), // This partially duplicates {url_alias}.alias, but contains the actual user // input, which should be stored per field, to make it an inherent part of - // an entity's life-cycle; e.g., also exposing a changed value in field - // revisions. Additionally, a change to the URL alias prefix in the field's - // instance settings should potentially trigger a bulk-update of existing - // aliases, using the same custom part but with a new prefix. + // an entity's life-cycle; e.g., exposing changed values in field revisions. 'value' => array( 'description' => 'The user-customizable part of the URL alias.', 'type' => 'varchar', diff --git a/core/modules/path/path.module b/core/modules/path/path.module index 0c3b316..a738f60 100644 --- a/core/modules/path/path.module +++ b/core/modules/path/path.module @@ -44,13 +44,6 @@ function path_field_info() { 'label' => t('URL alias'), 'description' => t('This field stores an URL alias in the database.'), 'default_widget' => 'path_default', - 'instance_settings' => array( - 'pattern' => array( - 'prefix' => NULL, - 'auto' => FALSE, - 'default' => NULL, - ), - ), ); return $types; } @@ -72,12 +65,7 @@ function path_field_insert($entity_type, $entity, $field, $instance, $langcode, $uri = $entity->uri(); $item['source'] = $uri['path']; $item['langcode'] = $langcode; - if (!empty($instance['settings']['pattern']['prefix'])) { - $item['alias'] = $instance['settings']['pattern']['prefix'] . '/' . $item['value']; - } - else { - $item['alias'] = $item['value']; - } + $item['alias'] = $item['value']; // path_save() populates $item['pid']. path_save($item); } @@ -99,19 +87,7 @@ function path_field_update($entity_type, $entity, $field, $instance, $langcode, $uri = $entity->uri(); $item['source'] = $uri['path']; $item['langcode'] = $langcode; - // The field widget might override the prefix, in case the prefix - // configured for the field instance is not part of the stored alias. - // @todo This looks wrong and will utterly fail when updating an entity - // programmatically, but not sure how to address the possibility differently. - if (isset($item['prefix'])) { - $item['alias'] = $item['prefix'] . $item['value']; - } - elseif (!empty($instance['settings']['pattern']['prefix'])) { - $item['alias'] = $instance['settings']['pattern']['prefix'] . '/' . $item['value']; - } - else { - $item['alias'] = $item['value']; - } + $item['alias'] = $item['value']; // path_save() populates $item['pid']. path_save($item); } @@ -130,67 +106,6 @@ function path_field_delete($entity_type, $entity, $field, $instance, $langcode, } /** - * Implements hook_field_instance_settings_form(). - */ -function path_field_instance_settings_form($field, $instance) { - $settings = $instance['settings']; - - if (!isset($settings['pattern']['prefix'])) { - $settings['pattern']['prefix'] = $instance['bundle']; - } - - $form['pattern'] = array( - '#type' => 'fieldset', - '#title' => t('URL alias pattern'), - '#collapsible' => FALSE, - '#element_validate' => array('path_field_instance_settings_form_pattern_validate'), - ); - $form['pattern']['prefix'] = array( - '#type' => 'textfield', - '#title' => t('Prefix'), - '#default_value' => isset($settings['pattern']['prefix']) ? $settings['pattern']['prefix'] : NULL, - '#maxlength' => 128, - '#description' => t('Enter a non-customizable URL alias prefix for all aliases of this field; e.g., !example.', array( - '!example' => "'" . check_plain($instance['entity_type'] . '/' . $instance['bundle']) . "'", - )), - ); - $form['pattern']['auto'] = array( - '#type' => 'checkbox', - '#title' => t('Automatically create alias'), - '#default_value' => isset($settings['pattern']['auto']) ? $settings['pattern']['auto'] : NULL, - // @todo Add token support. - '#access' => FALSE, - ); - $form['pattern']['default'] = array( - '#type' => 'textfield', - '#title' => t('Default alias'), - '#default_value' => isset($settings['pattern']['default']) ? $settings['pattern']['default'] : NULL, - '#maxlength' => 128, - // @todo Add token support. - '#access' => FALSE, - '#description' => t('@todo Output some example tokens specific to the entity type/bundle.'), - '#states' => array( - 'visible' => array( - ':input[name*="pattern][auto"]' => array('checked' => TRUE), - ), - ), - ); - return $form; -} - -/** - * #element_validate handler for 'pattern' in path_field_instance_settings_form(). - */ -function path_field_instance_settings_form_pattern_validate(&$element, &$form_state) { - // Ensure that the prefix does not contain a leading or trailing slash. - $element['prefix']['#value'] = trim($element['prefix']['#value'], '/'); - form_set_value($element['prefix'], $element['prefix']['#value'], $form_state); - - // @todo Validate that tokens in 'prefix' and 'default' exist. - // @todo Validate that 'default' contains at least one token. -} - -/** * Implements hook_permission(). */ function path_permission() {