MachineName.php in Machine name 8
File
src/Plugin/Field/FieldType/MachineName.php
View source
<?php
namespace Drupal\machine_name\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class MachineName extends FieldItemBase {
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Machine name'));
return $properties;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
public function getConstraints() {
$constraints = parent::getConstraints();
$constraint_manager = $this
->getTypedDataManager()
->getValidationConstraintManager();
$constraints[] = $constraint_manager
->create('MachineNameUnique', []);
return $constraints;
}
}
Classes
Name |
Description |
MachineName |
Plugin implementation of the 'machine_name' field type. |