Created
December 10, 2019 20:22
-
-
Save ericjgruber/166b265e50d1dc1978d28518c5990e57 to your computer and use it in GitHub Desktop.
Add a field to a Drupal 8 entity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Core\Entity\EntityTypeInterface; | |
use Drupal\link\LinkItemInterface; | |
/** | |
* Implements hook_entity_base_field_info(). | |
* In this example, you get the id of an existing entity, then add a field to it. | |
* Here, we create a link field that allows for link text, and then can be | |
* viewable on the output. | |
* | |
* This provides a handy way to alter existing modules, for example, without the need for a patch. | |
* Cool stuff. | |
*/ | |
function example_entity_base_field_info(EntityTypeInterface $entity_type) { | |
if ($entity_type->id() == 'some_existing_entity_id') { | |
$fields = []; | |
$fields['link'] = BaseFieldDefinition::create('link') | |
->setLabel(t('Learn More Link')) | |
->setDescription(t('The location the Learn More link points to.')) | |
->setRevisionable(TRUE) | |
->setRequired(TRUE) | |
->setSettings([ | |
'link_type' => LinkItemInterface::LINK_GENERIC, | |
'title' => DRUPAL_REQUIRED, | |
]) | |
->setDisplayOptions('form', [ | |
'type' => 'link_default', | |
'weight' => -2, | |
]) | |
->setDisplayOptions('view', [ | |
'type' => 'link', | |
'weight' => 0, | |
'label' => 'hidden' | |
]); | |
return $fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment