Commit c816d16c authored by juampynr's avatar juampynr Committed by Berdir

Issue #2660640 by juampynr, mariancalinro: Add bulk enable "Generate automatic...

Issue #2660640 by juampynr, mariancalinro: Add bulk enable "Generate automatic URL alias"/force option in bulk generate
parent 728d569e
......@@ -39,34 +39,16 @@ class PathautoState extends TypedData {
*/
public function getValue() {
if ($this->value === NULL) {
$entity = $this->parent->getEntity();
// @todo: Investigate why this happens.
if ($entity->isNew()) {
$this->value = static::CREATE;
return $this->value;
}
// If no value has been set or loaded yet, try to load a value if this
// entity has already been saved.
$this->value = \Drupal::keyValue($this->getCollection())
->get($this->parent->getEntity()->id());
// If it was not yet saved or no value was found, try to detect based on
// an existing alias if the entity is not new.
// If it was not yet saved or no value was found, then set the flag to
// create the alias if there is a matching pattern.
if ($this->value === NULL) {
$entity_path = '/' . $entity->toUrl()->getInternalPath();
$path = \Drupal::service('path.alias_manager')
->getAliasByPath(
$entity_path, $entity->language()->getId()
);
$pathauto_alias = \Drupal::service('pathauto.generator')
->createEntityAlias($entity, 'return');
if (($path != $entity_path && $path == $pathauto_alias)) {
$this->value = static::CREATE;
}
else {
$this->value = static::SKIP;
}
$entity = $this->parent->getEntity();
$pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
$this->value = !empty($pattern) ? static::CREATE : static::SKIP;
}
}
return $this->value;
......
......@@ -77,10 +77,8 @@ class PathautoBulkUpdateTest extends WebTestBase {
);
$this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
// This has generated 6 aliases. 5 nodes and one user that we created. There
// is also UID 1 but that user was created before the path field existed,
// so he does not have a pathauto state.
$this->assertText('Generated 6 URL aliases.');
// This has generated 7 aliases: 5 nodes and 2 users.
$this->assertText('Generated 7 URL aliases.');
// Check that aliases have actually been created.
foreach ($this->nodes as $node) {
......@@ -97,4 +95,28 @@ class PathautoBulkUpdateTest extends WebTestBase {
$this->assertNoEntityAliasExists($new_node);
}
/**
* Tests alias generation for nodes that existed before installing Pathauto.
*/
function testBulkUpdateExistingContent() {
// Create a node.
$node = $this->drupalCreateNode();
// Delete its alias and Pathauto metadata.
\Drupal::service('pathauto.alias_storage_helper')->deleteEntityPathAll($node);
$node->path->first()->get('pathauto')->purge();
\Drupal::entityManager()->getStorage('node')->resetCache(array($node->id()));
// Execute bulk generation.
// Bulk create aliases.
$edit = array(
'update[canonical_entities:node]' => TRUE,
);
$this->drupalPostForm('admin/config/search/path/update_bulk', $edit, t('Update'));
// Verify that the alias was created for the node.
$this->assertText('Generated 1 URL alias.');
}
}
......@@ -253,4 +253,39 @@ class PathautoNodeWebTest extends WebTestBase {
$this->assertNull(\Drupal::keyValue('pathauto_state.node')->get($node->id()), 'Pathauto state was deleted');
}
/**
* Tests that nodes without a Pathauto pattern can set custom aliases.
*/
public function testCustomAliasWithoutPattern() {
// First, delete all patterns to be sure that there will be no match.
$entity_ids = \Drupal::entityQuery('pathauto_pattern')->execute();
$entities = PathautoPattern::loadMultiple($entity_ids);
foreach ($entities as $entity) {
$entity->delete();
}
// Next, create a node with a custom alias.
$edit = [
'title[0][value]' => 'Sample article',
'path[0][alias]' => '/sample-article',
];
$this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
$this->assertText(t('article Sample article has been created.'));
// Test the alias.
$this->assertAliasExists(array('alias' => '/sample-article'));
$this->drupalGet('sample-article');
$this->assertResponse(200, 'A node without a pattern can have a custom alias.');
// Now create a node through the API.
$node = Node::create(['type' => 'article', 'title' => 'Sample article API', 'path' => ['alias' => '/sample-article-api']]);
$node->save();
// Test the alias.
$this->assertAliasExists(['alias' => '/sample-article-api']);
$this->drupalGet('sample-article-api');
$this->assertResponse(200);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment