imdb_api-1.0.x-dev/src/Plugin/Field/FieldType/ImdbItem.php

src/Plugin/Field/FieldType/ImdbItem.php
<?php

namespace Drupal\imdb_api\Plugin\Field\FieldType;

use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;

/**
 * Plugin implementation of the 'imdb_item' field type.
 *
 * @FieldType(
 *   id = "imdb_item",
 *   label = @Translation("IMDB item"),
 *   description = @Translation("IMDB item field type"),
 *   category = @Translation("IMDB item"),
 *   default_widget = "imdb_item",
 *   default_formatter = "imdb_item"
 * )
 */
class ImdbItem extends FieldItemBase {

  const DEFAULT_IMDB_ITEM = 'actor';

  /**
   * {@inheritdoc}
   */
  public static function defaultStorageSettings() {
    return [
      'max_length' => 255,
      'imdb_item_type' => self::DEFAULT_IMDB_ITEM,
      'is_ascii' => FALSE,
      'case_sensitive' => FALSE,
    ] + parent::defaultStorageSettings();
  }

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    // Prevent early t() calls by using the TranslatableMarkup.
    $properties['value'] = DataDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Text value'))
      ->setSetting('case_sensitive', $field_definition->getSetting('case_sensitive'))
      ->setRequired(TRUE);

    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $schema = [
      'columns' => [
        'value' => [
          'type' => $field_definition->getSetting('is_ascii') === TRUE ? 'varchar_ascii' : 'varchar',
          'length' => (int) $field_definition->getSetting('max_length'),
          'imdb_item_type' => $field_definition->getSetting('imdb_item_type'),
          'binary' => $field_definition->getSetting('case_sensitive'),
        ],
      ],
    ];

    return $schema;
  }

  /**
   * {@inheritdoc}
   */
  public function getConstraints() {
    $constraints = parent::getConstraints();

    if ($max_length = $this->getSetting('max_length')) {
      $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
      $constraints[] = $constraint_manager->create('ComplexData', [
        'value' => [
          'Length' => [
            'max' => $max_length,
            'maxMessage' => t('%name: may not be longer than @max characters.', [
              '%name' => $this->getFieldDefinition()->getLabel(),
              '@max' => $max_length
            ]),
          ],
        ],
      ]);
    }

    return $constraints;
  }

  /**
   * {@inheritdoc}
   */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $random = new Random();
    $values['value'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length')));
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
    $elements = [];

    // @TODO get rid of hardcoded options.
    $elements['imdb_item_type'] = [
      '#type' => 'select',
      '#title' => t('IMDB item type'),
      '#options' => [
        'actor' => $this->t('Actor'),
        'movie' => $this->t('Movie'),
        'episode' => $this->t('Episode'),
        'tv_show' => $this->t('TV Show'),
        'video_game' => $this->t('Video Game'),
      ],
      '#default_value' => $this->getSetting('imdb_item_type'),
      '#required' => TRUE,
      '#disabled' => $has_data,
    ];

    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('value')->getValue();
    return $value === NULL || $value === '';
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc