0

I'm trying to figure out how to create a custom compound field, but I'm not quite sure how to add a file field to it. There are plenty of tuts on creating compound fields that are comprised of text fields, but I can't find any that talk about combining text and file fields. I guess the main problem is that I don't understand how the 'schema' function would work, since it refers to Drupal data types, but the data uploaded via the file field won't be stored in the Drupal database, but rather stored on disk, as any other uploaded file would be.

Can someone point me in the right direction?

1 Answer 1

0

A file field is an entity reference field pointing to the File entity (or in D8.4 to the Media entity). To build such a file field extend FileItem (which parent is EntityReferenceItem). An example for this would be the image field ImageItem, which is a compound field adding properties like title, alt, height and width:

ImageItem::propertyDefinitions()

public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties = parent::propertyDefinitions($field_definition);

    unset($properties['display']);
    unset($properties['description']);

    $properties['alt'] = DataDefinition::create('string')
      ->setLabel(t('Alternative text'))
      ->setDescription(t("Alternative image text, for the image's 'alt' attribute."));

    $properties['title'] = DataDefinition::create('string')
      ->setLabel(t('Title'))
      ->setDescription(t("Image title text, for the image's 'title' attribute."));

    $properties['width'] = DataDefinition::create('integer')
      ->setLabel(t('Width'))
      ->setDescription(t('The width of the image in pixels.'));

    $properties['height'] = DataDefinition::create('integer')
      ->setLabel(t('Height'))
      ->setDescription(t('The height of the image in pixels.'));

    return $properties;
  }

The properties for the referencing part are inherited from the parent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.