123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace Drupal\materio_samples\Plugin\Field\FieldType;
- use Drupal\Core\Field\FieldItemBase;
- use Drupal\Core\Field\FieldStorageDefinitionInterface;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\TypedData\DataDefinition;
- use Drupal\Core\TypedData\DataReferenceTargetDefinition;
- use Drupal\Core\StringTranslation\TranslatableMarkup;
- /**
- * Plugin implementation of the 'field_example_rgb' field type.
- *
- * @FieldType(
- * id = "materio_samples_field",
- * label = @Translation("Samples"),
- * module = "materio_samples",
- * category = "Materio",
- * description = @Translation("Provide a sample field, each showroom (taxonomy term) can fill it's own sample reference, and only it's own."),
- * default_widget = "materio_samples_default_widget",
- * default_formatter = "materio_samples_default_formatter"
- * )
- */
- class SamplesItem extends FieldItemBase {
- /**
- * {@inheritdoc}
- */
- public static function schema(FieldStorageDefinitionInterface $field_definition) {
- return [
- 'columns' => [
- 'location' => [
- 'description' => "The actual location reference in the showroom.",
- 'type' => 'text',
- 'size' => 'tiny',
- // 'not null' => FALSE,
- ],
- 'target_id' => [
- 'description' => 'the id of the target taxonomy term.',
- 'type' => 'int',
- 'unsigned' => TRUE
- ]
- ],
- 'indexes' => [
- 'target_id' => ['target_id'],
- ],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function isEmpty() {
- $value = $this->get('location')->getValue();
- return $value === NULL || $value === '';
- }
- /**
- * {@inheritdoc}
- */
- public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
- $properties['location'] = DataDefinition::create('string')
- ->setLabel(t('Reference'));
- $properties['target_id'] = DataReferenceTargetDefinition::create('integer')
- // ->setLabel(new TranslatableMarkup('@label ID', ['@label' => $target_type_info->getLabel()]))
- ->setLabel(new TranslatableMarkup('@label ID', ['@label' => 'Showroom'])) // get voc name from settings
- ->setSetting('unsigned', TRUE)
- ->setRequired(TRUE);
- return $properties;
- }
- /**
- * {@inheritdoc}
- */
- public static function defaultFieldSettings() {
- return [
- // Declare a single setting, 'size', with a default
- // value of 'large'
- 'vid' => null,
- ] + parent::defaultFieldSettings();
- }
- /**
- * {@inheritdoc}
- */
- public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
- // dsm($form);
- // get vocabularies
- $vocabularies = \Drupal\taxonomy\Entity\Vocabulary::loadMultiple();
- // dsm($vocabularies);
- $options = [null => "choose"];
- foreach ($vocabularies as $vid => $voc) {
- $options[$voc->id()] = $voc->label();
- }
- // dsm($options);
- $element = [];
- // The key of the element should be the setting name
- $element['vid'] = [
- '#title' => $this->t('Vocabulary'),
- '#type' => 'select',
- '#options' => $options,
- '#default_value' => $this->getSetting('vid'),
- ];
- return $element;
- }
- /**
- * Form element validation handler; Invokes selection plugin's validation.
- *
- * @param array $form
- * The form where the settings form is being included in.
- * @param \Drupal\Core\Form\FormStateInterface $form_state
- * The form state of the (entire) configuration form.
- */
- public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) {
- $field = $form_state->getFormObject()->getEntity();
- dsm($field);
- }
- }
|