You are here

function easy_email_field_add_attachment_field in Easy Email 8

Same name and namespace in other branches
  1. 2.0.x easy_email.module \easy_email_field_add_attachment_field()
1 call to easy_email_field_add_attachment_field()
easy_email_entity_insert in ./easy_email.module

File

./easy_email.module, line 566
Contains easy_email.module.

Code

function easy_email_field_add_attachment_field(EasyEmailTypeInterface $easy_email_type) {
  $field_definition = BaseFieldDefinition::create('file')
    ->setTargetEntityTypeId('easy_email')
    ->setTargetBundle($easy_email_type
    ->id())
    ->setName('attachment')
    ->setLabel(t('Attachments'))
    ->setRevisionable(TRUE)
    ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
    ->setRequired(FALSE)
    ->setSetting('file_directory', 'attachments')
    ->setSetting('file_extensions', 'txt pdf doc docx xls xlsx ppt pptx rtf png jpg jpeg gif')
    ->setSetting('max_filesize', '')
    ->setSetting('description_field', FALSE)
    ->setSetting('handler', 'default')
    ->setSetting('uri_scheme', 'private')
    ->setTranslatable(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'file_generic',
    'weight' => 0,
  ])
    ->setDisplayOptions('view', [
    'type' => 'file_default',
    'weight' => 0,
    'label' => 'hidden',
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

  /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
  $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
  $scheme_options = $stream_wrapper_manager
    ->getNames(StreamWrapperInterface::WRITE_VISIBLE);
  if (!empty($scheme_options['private'])) {
    $field_definition
      ->setSetting('uri_scheme', 'private');
  }
  elseif (!empty($scheme_options['public'])) {
    $field_definition
      ->setSetting('uri_scheme', 'public');
  }
  try {
    easy_email_create_field($field_definition, FALSE);
  } catch (\RuntimeException $e) {

    // In this case, field already exists from installation
  } catch (EntityStorageException $e) {

    // In this case, field already exists from installation
  }
}