Created
November 3, 2022 14:23
-
-
Save nickopris/63e77efd5e514ed626403194127126c6 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* @file Contains \Drupal\custom_module\Plugin\Field\FieldType\RoleRate | |
*/ | |
namespace Drupal\custom_module\Plugin\Field\FieldType; | |
use Drupal\Core\Field\FieldStorageDefinitionInterface; | |
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\TypedData\DataDefinition; | |
/** | |
* Plugin implementation of the 'RoleRate' field type. | |
* | |
* @FieldType ( | |
* id = "rolerate", | |
* label = @Translation("Entity reference RoleRate"), | |
* description = @Translation("Stores the daily rate of any group role member."), | |
* category = @Translation("Reference"), | |
* default_widget = "entity_reference_autocomplete", | |
* default_formatter = "entity_reference_label", | |
* list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList", | |
* ) | |
*/ | |
class RoleRate extends EntityReferenceItem { | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function defaultStorageSettings() { | |
return [ | |
'target_type' => 'group_role', | |
] + parent::defaultStorageSettings(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { | |
$properties = parent::propertyDefinitions($field_definition); | |
$properties['role_rate'] = DataDefinition::create('integer') | |
->setLabel($field_definition->getSetting('role_rate_label')) | |
->setRequired(TRUE); | |
return $properties; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function schema(FieldStorageDefinitionInterface $field_definition) { | |
$schema = parent::schema($field_definition); | |
$schema['columns']['role_rate'] = [ | |
'type' => 'int', | |
]; | |
return $schema; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function defaultFieldSettings() { | |
return [ | |
'role_rate_label' => t('Role rate'), | |
] + parent::defaultFieldSettings(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function fieldSettingsForm(array $form, FormStateInterface $form_state) { | |
$elements = parent::fieldSettingsForm($form, $form_state); | |
$elements['role_rate_label'] = [ | |
'#type' => 'textfield', | |
'#title' => t('Role rate label'), | |
'#default_value' => $this->getSetting('role_rate_label'), | |
'#description' => $this->t('Also used as a placeholder in multi-value instances.'), | |
]; | |
return $elements; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getPreconfiguredOptions() { | |
// In the base EntityReference class, this is used to populate the | |
// list of field-types with options for each destination entity type. | |
// Too much work, we'll just make people fill that out later. | |
// Also, keeps the field type dropdown from getting too cluttered. | |
return []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment