skillset_inview-8.x-1.x-dev/src/Plugin/Field/FieldType/SkillsetItem.php
src/Plugin/Field/FieldType/SkillsetItem.php
<?php
namespace Drupal\skillset_inview\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
/**
* Plugin implementation of the 'skillset_item' field type.
*
* @FieldType(
* id = "skillset_item",
* label = @Translation("Skillset Item"),
* module = "skillset_inview",
* description = @Translation("Stores a title and a percentage value for a skillset."),
* default_widget = "skillset_item_widget",
* default_formatter = "skillset_item_formatter"
* )
*/
class SkillsetItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'title' => [
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
],
'percentage' => [
'type' => 'float',
'not null' => FALSE,
],
],
];
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$title = $this->get('title')->getValue();
$percentage = $this->get('percentage')->getValue();
return $title === NULL || $title === '' || $percentage === NULL;
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['title'] = DataDefinition::create('string')
->setLabel(t('Title'))
->setRequired(TRUE);
$properties['percentage'] = DataDefinition::create('float')
->setLabel(t('Percentage'))
->setRequired(TRUE);
return $properties;
}
/**
* {@inheritdoc}
*/
public function getValue() {
return [
'title' => $this->get('title')->getValue(),
'percentage' => $this->get('percentage')->getValue(),
];
}
/**
* {@inheritdoc}
*/
public function setValue($values, $notify = TRUE) {
$this->set('title', $values['title']);
$this->set('percentage', $values['percentage']);
parent::setValue($values, $notify);
}
/**
* {@inheritdoc}
*/
public function setProperty($property_name, $value) {
parent::setProperty($property_name, $value);
}
}
