Commit 2951df5d authored by catch's avatar catch
Browse files

Issue #3094366 by mohit_aghera, KittenDestroyer, Berdir, rensingh99: Error...

Issue #3094366 by mohit_aghera, KittenDestroyer, Berdir, rensingh99: Error when saving config entity with "Link to entity" checked if field formatters
parent 4676911f
...@@ -92,14 +92,15 @@ public static function defaultSettings() { ...@@ -92,14 +92,15 @@ public static function defaultSettings() {
*/ */
public function settingsForm(array $form, FormStateInterface $form_state) { public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state); $form = parent::settingsForm($form, $form_state);
$entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId()); $entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
$form['link_to_entity'] = [ if ($entity_type->hasLinkTemplate('canonical')) {
'#type' => 'checkbox', $form['link_to_entity'] = [
'#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]), '#type' => 'checkbox',
'#default_value' => $this->getSetting('link_to_entity'), '#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]),
]; '#default_value' => $this->getSetting('link_to_entity'),
];
}
return $form; return $form;
} }
...@@ -111,7 +112,9 @@ public function settingsSummary() { ...@@ -111,7 +112,9 @@ public function settingsSummary() {
$summary = []; $summary = [];
if ($this->getSetting('link_to_entity')) { if ($this->getSetting('link_to_entity')) {
$entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId()); $entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
$summary[] = $this->t('Linked to the @entity_label', ['@entity_label' => $entity_type->getLabel()]); if ($entity_type->hasLinkTemplate('canonical')) {
$summary[] = $this->t('Linked to the @entity_label', ['@entity_label' => $entity_type->getLabel()]);
}
} }
return $summary; return $summary;
} }
...@@ -122,8 +125,11 @@ public function settingsSummary() { ...@@ -122,8 +125,11 @@ public function settingsSummary() {
public function viewElements(FieldItemListInterface $items, $langcode) { public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = []; $elements = [];
$url = NULL; $url = NULL;
if ($this->getSetting('link_to_entity')) { $entity = $items->getEntity();
$url = $this->getEntityUrl($items->getEntity()); $entity_type = $entity->getEntityType();
if ($this->getSetting('link_to_entity') && !$entity->isNew() && $entity_type->hasLinkTemplate('canonical')) {
$url = $this->getEntityUrl($entity);
} }
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\entity_test\Entity\EntityTestLabel;
use Drupal\entity_test\Entity\EntityTestRev; use Drupal\entity_test\Entity\EntityTestRev;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
...@@ -67,6 +68,7 @@ protected function setUp(): void { ...@@ -67,6 +68,7 @@ protected function setUp(): void {
// Configure the theme system. // Configure the theme system.
$this->installConfig(['system', 'field']); $this->installConfig(['system', 'field']);
$this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_rev');
$this->installEntitySchema('entity_test_label');
$this->entityType = 'entity_test_rev'; $this->entityType = 'entity_test_rev';
$this->bundle = $this->entityType; $this->bundle = $this->entityType;
...@@ -185,4 +187,46 @@ public function testStringFormatter() { ...@@ -185,4 +187,46 @@ public function testStringFormatter() {
$this->assertLinkByHref($entity->toUrl('canonical')->toString()); $this->assertLinkByHref($entity->toUrl('canonical')->toString());
} }
/**
* Test "link_to_entity" feature on fields which are added to config entity.
*/
public function testLinkToContentForEntitiesWithNoCanonicalPath() {
$this->enableModules(['entity_test']);
$field_name = 'test_field_name';
$entity_type = $bundle = 'entity_test_label';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type,
'type' => 'string',
]);
$field_storage->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $entity_type,
'label' => $this->randomMachineName(),
]);
$instance->save();
$display = \Drupal::service('entity_display.repository')
->getViewDisplay($entity_type, $bundle)
->setComponent($field_name, [
'type' => 'string',
'settings' => [
'link_to_entity' => TRUE,
],
'region' => 'content',
]);
$display->save();
$value = $this->randomMachineName();
$entity = EntityTestLabel::create(['name' => 'test']);
$entity->{$field_name}->value = $value;
$entity->save();
$this->renderEntityFields($entity, $display);
$this->assertRaw($value);
}
} }
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