FileItem.php

Contains \Drupal\file\Type\FileItem.

Namespace

Drupal\file\Type

File

drupal/core/modules/file/lib/Drupal/file/Type/FileItem.php
View source
<?php

/**
 * @file
 * Contains \Drupal\file\Type\FileItem.
 */
namespace Drupal\file\Type;

use Drupal\Core\Entity\Field\FieldItemBase;
use Drupal\Core\TypedData\TypedDataInterface;

/**
 * Defines the 'file_field' entity field item.
 */
class FileItem extends FieldItemBase {

  /**
   * Property definitions of the contained properties.
   *
   * @see FileItem::getPropertyDefinitions()
   *
   * @var array
   */
  static $propertyDefinitions;

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
   */
  public function getPropertyDefinitions() {
    if (!isset(static::$propertyDefinitions)) {
      static::$propertyDefinitions['fid'] = array(
        'type' => 'integer',
        'label' => t('Referenced file id.'),
      );
      static::$propertyDefinitions['display'] = array(
        'type' => 'boolean',
        'label' => t('Flag to control whether this file should be displayed when viewing content.'),
      );
      static::$propertyDefinitions['description'] = array(
        'type' => 'string',
        'label' => t('A description of the file.'),
      );
      static::$propertyDefinitions['entity'] = array(
        'type' => 'entity',
        'constraints' => array(
          'EntityType' => 'file',
        ),
        'label' => t('File'),
        'description' => t('The referenced file'),
        // The entity object is computed out of the fid.
        'computed' => TRUE,
        'read-only' => FALSE,
        'settings' => array(
          'id source' => 'fid',
        ),
      );
    }
    return static::$propertyDefinitions;
  }

  /**
   * Overrides \Drupal\Core\Entity\Field\FieldItemBase::get().
   */
  public function setValue($values, $notify = TRUE) {

    // Treat the values as property value of the entity property, if no array is
    // given.
    if (isset($values) && !is_array($values)) {
      $values = array(
        'entity' => $values,
      );
    }

    // Make sure that the 'entity' property gets set as 'fid'.
    if (isset($values['fid']) && !isset($values['entity'])) {
      $values['entity'] = $values['fid'];
    }
    parent::setValue($values, $notify);
  }

}

Classes

Namesort descending Description
FileItem Defines the 'file_field' entity field item.