Issue #2057973 by aspilicious, Wim Leers: Don't add Edit module's data- attributes on pseudo fields (as used e.g. by Display Suite).

8.0.x
webchick 2013-10-05 00:33:07 -07:00
parent 0a90af252e
commit fdf3858b27
3 changed files with 53 additions and 1 deletions

View File

@ -182,7 +182,13 @@ function edit_field_formatter_info_alter(&$info) {
function edit_preprocess_field(&$variables) {
$element = $variables['element'];
$entity = $element['#object'];
$variables['attributes']['data-edit-id'] = $entity->entityType() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode'];
// Fields that are not part of the entity (i.e. dynamically injected "pseudo
// fields") and computed fields are not editable.
$definition = $entity->getPropertyDefinition($element['#field_name']);
if ($definition && empty($definition['computed'])) {
$variables['attributes']['data-edit-id'] = $entity->entityType() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode'];
}
}
/**

View File

@ -291,4 +291,17 @@ class EditLoadingTest extends WebTestBase {
}
}
/**
* Tests that Edit doesn't make pseudo fields or computed fields editable.
*/
function testPseudoFields() {
\Drupal::moduleHandler()->install(array('edit_test'));
$this->drupalLogin($this->author_user);
$this->drupalGet('node/1');
// Check that the data- attribute is not added.
$this->assertNoRaw('data-edit-id="node/1/edit_test_pseudo_field/und/default"');
}
}

View File

@ -4,3 +4,36 @@
* @file
* Helper module for the Edit tests.
*/
use Drupal\Core\Language\Language;
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity\Entity\EntityDisplay;
/**
* Implements hook_entity_view_alter().
*/
function edit_test_entity_view_alter(&$build, EntityInterface $entity, EntityDisplay $display) {
if ($entity->entityType() == 'node' && $entity->bundle() == 'article') {
$build['pseudo'] = array(
'#theme' => 'field',
'#title' => 'My pseudo field',
'#field_name' => 'edit_test_pseudo_field',
'#label_display' => 'Label',
'#entity_type' => $entity->entityType(),
'#bundle' => $entity->bundle(),
'#language' => Language::LANGCODE_NOT_SPECIFIED,
'#field_type' => 'pseudo',
'#view_mode' => 'default',
'#object' => $entity,
'#access' => TRUE,
'#items' => array(
0 => array(
'value' => 'pseudo field',
),
),
0 => array(
'#markup' => 'pseudo field',
),
);
}
}