use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\StringItemBase;
/**
* Defines the 'string_medium' field type.
*
* @FieldType(
* id = "string_medium",
* label = @Translation("Text (plain, medium)"),
* description = @Translation("A field containing a medium string value."),
* category = @Translation("Text"),
* default_widget = "string_textarea",
* default_formatter = "basic_string",
* )
*/
class StringMediumItem extends StringItemBase {
Comments9
entity_reference define sorting?
Great article - really useful info here that is hard to find anywhere else.
Is is possible to define the sort order of the entity_reference in the BaseFieldDefinition?
If it's not possible in the BaseFieldDefinition then where should I define the sorting?
default sort ordering
After some further digging this seems to be the key bit of code:
->setSetting('handler_settings', [ 'sort' => [ 'field' => 'code', 'direction' => 'ASC', ] ])Thank you
Thank you Felix
Great info
Keep getting back to this as a reference, great info!
Thank you.
Tmestamp example
Please, add timestamp example ;)
$fields['timestamp'] =…
$fields['timestamp'] = BaseFieldDefinition::create('created')
->setLabel(new TranslatableMarkup('Created On'))
->setDescription(t('The time that the node was created.'))
->setDisplayOptions('view', [
'weight' => -2,
'type' => 'timestamp',
'settings' => [
'date_format' => 'custom',
'custom_date_format' => 'd/m/Y H:i:s',
]
])
->setDisplayConfigurable('view', TRUE);
Medium
Thx !
Have you find how to use medium size data fields in long_string ?
Oh yeah ! Find by myself =)…
Oh yeah ! Find by myself =).
<?phpnamespace Drupal\monitoring\Plugin\Field\FieldType;use Drupal\Component\Utility\Random;use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\StringItemBase;
/*** Defines the 'string_medium' field type.
*
* @FieldType(
* id = "string_medium",
* label = @Translation("Text (plain, medium)"),
* description = @Translation("A field containing a medium string value."),
* category = @Translation("Text"),
* default_widget = "string_textarea",
* default_formatter = "basic_string",
* )
*/
class StringMediumItem extends StringItemBase {
/*** {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'text',
'size' => 'medium',
],
],
];
}
/*** {@inheritdoc}
*/
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$random = new Random();
$values['value'] = $random->paragraphs();
return $values;
}
}I really love D8 !
How to set datetime
How to add datetime field type