View source
<?php
namespace Drupal\simplenews\Plugin\Field\FieldType;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class IssueItem extends EntityReferenceItem {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = parent::propertyDefinitions($field_definition);
$properties['handler'] = DataDefinition::create('string')
->setLabel(t('Handler'));
$properties['handler_settings'] = MapDataDefinition::create()
->setLabel(t('Handler settings'));
$properties['status'] = DataDefinition::create('integer')
->setLabel(t('Status'))
->setSetting('unsigned', TRUE);
$properties['sent_count'] = DataDefinition::create('integer')
->setLabel(t('Sent count'))
->setSetting('unsigned', TRUE);
$properties['subscribers'] = DataDefinition::create('integer')
->setLabel(t('Subscribers'))
->setSetting('unsigned', TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = parent::schema($field_definition);
$schema['columns']['handler'] = array(
'description' => 'The issue handler.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
);
$schema['columns']['handler_settings'] = array(
'description' => 'The issue handler settings.',
'type' => 'blob',
'size' => 'big',
'not null' => FALSE,
'serialize' => TRUE,
);
$schema['columns']['status'] = array(
'description' => 'A flag indicating whether the issue is published (3), ready (2), pending (1) or not (0).',
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
);
$schema['columns']['sent_count'] = array(
'description' => 'Counter of already sent newsletters.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
);
$schema['columns']['subscribers'] = array(
'description' => 'Counter of subscribers.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
);
return $schema;
}
public function setValue($values, $notify = TRUE) {
if (count($values) == 1 && isset($values['target_id'])) {
$values = array_merge($this->values, $values);
if (!isset($values['handler']) || $values['handler'] == NULL) {
$values['handler'] = 'simplenews_all';
}
if (!isset($values['status']) || $values['status'] == NULL) {
$values['status'] = 0;
}
if (!isset($values['sent_count']) || $values['sent_count'] == NULL) {
$values['sent_count'] = 0;
}
if (!isset($values['subscribers']) || $values['subscribers'] == NULL) {
$values['subscribers'] = 0;
}
}
parent::setValue($values, $notify);
}
}