Issue #3094366 by mohit_aghera, KittenDestroyer, Berdir, rensingh99: Error when saving config entity with "Link to entity" checked if field formatters
parent
4676911fd9
commit
2951df5de3
|
@ -92,14 +92,15 @@ class StringFormatter extends FormatterBase {
|
|||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::settingsForm($form, $form_state);
|
||||
|
||||
$entity_type = $this->entityTypeManager->getDefinition($this->fieldDefinition->getTargetEntityTypeId());
|
||||
|
||||
$form['link_to_entity'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]),
|
||||
'#default_value' => $this->getSetting('link_to_entity'),
|
||||
];
|
||||
if ($entity_type->hasLinkTemplate('canonical')) {
|
||||
$form['link_to_entity'] = [
|
||||
'#type' => 'checkbox',
|
||||
'#title' => $this->t('Link to the @entity_label', ['@entity_label' => $entity_type->getLabel()]),
|
||||
'#default_value' => $this->getSetting('link_to_entity'),
|
||||
];
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
@ -111,7 +112,9 @@ class StringFormatter extends FormatterBase {
|
|||
$summary = [];
|
||||
if ($this->getSetting('link_to_entity')) {
|
||||
$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;
|
||||
}
|
||||
|
@ -122,8 +125,11 @@ class StringFormatter extends FormatterBase {
|
|||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = [];
|
||||
$url = NULL;
|
||||
if ($this->getSetting('link_to_entity')) {
|
||||
$url = $this->getEntityUrl($items->getEntity());
|
||||
$entity = $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) {
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Drupal\Tests\field\Kernel\String;
|
|||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\entity_test\Entity\EntityTestLabel;
|
||||
use Drupal\entity_test\Entity\EntityTestRev;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
@ -67,6 +68,7 @@ class StringFormatterTest extends KernelTestBase {
|
|||
// Configure the theme system.
|
||||
$this->installConfig(['system', 'field']);
|
||||
$this->installEntitySchema('entity_test_rev');
|
||||
$this->installEntitySchema('entity_test_label');
|
||||
|
||||
$this->entityType = 'entity_test_rev';
|
||||
$this->bundle = $this->entityType;
|
||||
|
@ -185,4 +187,46 @@ class StringFormatterTest extends KernelTestBase {
|
|||
$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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue