SoundCloudItem.php in SoundCloud field 8
File
src/Plugin/Field/FieldType/SoundCloudItem.php
View source
<?php
namespace Drupal\soundcloudfield\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class SoundCloudItem extends FieldItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['url'] = DataDefinition::create('uri')
->setLabel(t('URL'));
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'url' => array(
'description' => 'The URL of the SoundCloud link.',
'type' => 'varchar',
'length' => 2048,
'not null' => FALSE,
),
),
);
}
public function instanceSettingsForm(array $form, array &$form_state) {
$element = array();
return $element;
}
public function isEmpty() {
$value = $this
->get('url')
->getValue();
return $value === NULL || $value === '';
}
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints = parent::getConstraints();
$max_length = 512;
$constraints[] = $constraint_manager
->create('ComplexData', array(
'url' => array(
'Length' => array(
'max' => $max_length,
'maxMessage' => $this
->t('%name: the SoundCloud URL may not be longer than @max characters.', array(
'%name' => $this
->getFieldDefinition()
->getLabel(),
'@max' => $max_length,
)),
),
),
));
return $constraints;
}
}