Commit a02c8a61 authored by webchick's avatar webchick

Issue #2916809 by seanB, robpowell, phenaproxima, borisson_, Berdir, yoroy:...

Issue #2916809 by seanB, robpowell, phenaproxima, borisson_, Berdir, yoroy: Add fieldset/vertical tab for URL alias field

(cherry picked from commit 954f7d59)
parent b4a02f80
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
......@@ -37,28 +38,6 @@ function path_help($route_name, RouteMatchInterface $route_match) {
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
*/
function path_form_node_form_alter(&$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
$form['path_settings'] = [
'#type' => 'details',
'#title' => t('URL path settings'),
'#open' => !empty($form['path']['widget'][0]['alias']['#value']),
'#group' => 'advanced',
'#access' => !empty($form['path']['#access']) && $node->hasField('path') && $node->get('path')->access('edit'),
'#attributes' => [
'class' => ['path-form'],
],
'#attached' => [
'library' => ['path/drupal.path'],
],
'#weight' => 30,
];
$form['path']['#group'] = 'path_settings';
}
/**
* Implements hook_entity_base_field_info().
*/
......
......@@ -49,6 +49,27 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#type' => 'value',
'#value' => $items[$delta]->langcode,
];
// If the advanced settings tabs-set is available (normally rendered in the
// second column on wide-resolutions), place the field as a details element
// in this tab-set.
if (isset($form['advanced'])) {
$element += [
'#type' => 'details',
'#title' => t('URL path settings'),
'#open' => !empty($items[$delta]->alias),
'#group' => 'advanced',
'#access' => $entity->get('path')->access('edit'),
'#attributes' => [
'class' => ['path-form'],
],
'#attached' => [
'library' => ['path/drupal.path'],
],
];
$element['#weight'] = 30;
}
return $element;
}
......
<?php
namespace Drupal\Tests\path\Functional;
use Drupal\media\Entity\MediaType;
/**
* Tests the path media form UI.
*
* @group path
*/
class PathMediaFormTest extends PathTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['media', 'media_test_source'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create test user and log in.
$web_user = $this->drupalCreateUser(['create media', 'create url aliases']);
$this->drupalLogin($web_user);
}
/**
* Tests the media form UI.
*/
public function testMediaForm() {
$assert_session = $this->assertSession();
// Create media type.
$media_type_id = 'foo';
$media_type = MediaType::create([
'id' => $media_type_id,
'label' => $media_type_id,
'source' => 'test',
'source_configuration' => [],
'field_map' => [],
'new_revision' => FALSE,
]);
$media_type->save();
$this->drupalGet('media/add/' . $media_type_id);
// Make sure we have a vertical tab fieldset and 'Path' field.
$assert_session->elementContains('css', '.form-type-vertical-tabs #edit-path-0 summary', 'URL alias');
$assert_session->fieldExists('path[0][alias]');
// Disable the 'Path' field for this content type.
entity_get_form_display('media', $media_type_id, 'default')
->removeComponent('path')
->save();
$this->drupalGet('media/add/' . $media_type_id);
// See if the whole fieldset is gone now.
$assert_session->elementNotExists('css', '.form-type-vertical-tabs #edit-path-0');
$assert_session->fieldNotExists('path[0][alias]');
}
}
......@@ -28,13 +28,15 @@ protected function setUp() {
* Tests the node form ui.
*/
public function testNodeForm() {
$assert_session = $this->assertSession();
$this->drupalGet('node/add/page');
// Make sure we have a Path fieldset and Path fields.
$this->assertRaw(' id="edit-path-settings"', 'Path settings details exists');
$this->assertFieldByName('path[0][alias]', NULL, 'Path alias field exists');
// Make sure we have a vertical tab fieldset and 'Path' fields.
$assert_session->elementContains('css', '.form-type-vertical-tabs #edit-path-0 summary', 'URL alias');
$assert_session->fieldExists('path[0][alias]');
// Disable the Path field for this content type.
// Disable the 'Path' field for this content type.
entity_get_form_display('node', 'page', 'default')
->removeComponent('path')
->save();
......@@ -42,8 +44,8 @@ public function testNodeForm() {
$this->drupalGet('node/add/page');
// See if the whole fieldset is gone now.
$this->assertNoRaw(' id="edit-path-settings"', 'Path settings details does not exist');
$this->assertNoFieldByName('path[0][alias]', NULL, 'Path alias field does not exist');
$assert_session->elementNotExists('css', '.form-type-vertical-tabs #edit-path-0');
$assert_session->fieldNotExists('path[0][alias]');
}
}
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