You are here

public static function FillPdfForm::baseFieldDefinitions in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Entity/FillPdfForm.php \Drupal\fillpdf\Entity\FillPdfForm::baseFieldDefinitions()

Provides base field definitions for an entity type.

Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:

$fields['name'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Name'));

By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().

The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.

Overrides ContentEntityBase::baseFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()

File

src/Entity/FillPdfForm.php, line 73

Class

FillPdfForm
Defines the entity for managing uploaded FillPDF forms.

Namespace

Drupal\fillpdf\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = [];
  $fields['fid'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('FillPDF Form ID'))
    ->setDescription(t('The ID of the FillPdfForm entity.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('UUID'))
    ->setDescription(t('The UUID of the FillPdfForm entity.'))
    ->setReadOnly(TRUE);
  $fields['file'] = BaseFieldDefinition::create('file')
    ->setLabel(t('The associated managed file.'))
    ->setSetting('file_extensions', 'pdf')
    ->setDescription(t('The associated managed file.'));
  $overview_url = Url::fromUri('base://admin/structure/fillpdf')
    ->toString();
  $fields['admin_title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Administrative title'))
    ->setDescription(t('Enter an administrative title to help identifying this FillPDF Form on the <a href="@overview_url">form overview page</a> and in some other places.', [
    '@overview_url' => $overview_url,
  ]))
    ->setDisplayOptions('form', [
    'type' => 'string',
    'weight' => 0,
  ]);
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Filename pattern'))
    ->setDescription(new TranslatableMarkup('This pattern will be used for deciding the filename of your PDF. This field supports tokens.'))
    ->setDisplayOptions('form', [
    'type' => 'string',
    'weight' => 10,
  ]);

  // Form element is set up in FillPdfFormForm.
  $fields['default_entity_type'] = BaseFieldDefinition::create('string');

  // Form element is set up in FillPdfFormForm.
  $fields['default_entity_id'] = BaseFieldDefinition::create('integer');
  $fields['destination_path'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Destination path'))
    ->setDescription(new TranslatableMarkup('You may specify a subdirectory for storing filled PDFs. This field supports tokens.'))
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 21,
    'settings' => [
      'size' => 38,
    ],
  ]);

  // @todo: add post_save_redirect field for where to send the browser by default after they generate a PDF
  $fields['scheme'] = BaseFieldDefinition::create('list_string')
    ->setLabel('File storage')
    ->setSettings([
    'allowed_values_function' => [
      static::class,
      'getStorageSchemeOptions',
    ],
  ])
    ->setDefaultValueCallback(static::class . '::getStorageSchemeDefault')
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 20,
  ]);
  $fields['destination_redirect'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Redirect browser directly to saved PDF'))
    ->setDescription(t("Instead of redirecting your visitors to the front page, this will redirect them directly to the PDF. However, if you pass Drupal's <em>destination</em> query string parameter, that will override this setting."))
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'weight' => 30,
    'settings' => [
      'display_label' => TRUE,
    ],
  ]);
  $fields['replacements'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Change text before sending to PDF (Transform values)'))
    ->setDescription(FillPdfAdminFormHelper::getReplacementsDescription())
    ->setDisplayOptions('form', [
    'type' => 'string_long',
    'weight' => 40,
  ]);
  $fields['pdftk_encryption'] = BaseFieldDefinition::create('list_string')
    ->setLabel('PDFtk encryption strength')
    ->setDescription("Select the type of PDFtk encryption you'd like to use. You should choose 128-bit unless you know otherwise.")
    ->setCardinality(1)
    ->setSettings([
    'allowed_values_function' => [
      PdftkPdfBackend::class,
      'getEncryptionOptions',
    ],
  ])
    ->setDefaultValue(NULL)
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 50,
  ]);
  $fields['permissions'] = BaseFieldDefinition::create('list_string')
    ->setLabel('User permissions')
    ->setCardinality(-1)
    ->setDescription('Choose the permissions the user should have for the encrypted PDF. If they enter the Owner Password, they will be able to unlock it. <strong>If you do not specify any permissions, then none of these operations will be allowed.</strong>')
    ->setSettings([
    'allowed_values_function' => [
      PdftkPdfBackend::class,
      'getUserPermissionList',
    ],
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_buttons',
    'weight' => 60,
  ]);
  $fields['owner_password'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Owner password'))
    ->setDescription(new TranslatableMarkup('Required for encryption. Enter the decryption password for the PDF. This password allows PDF security settings to be changed. If you configure encryption and permissions but leave this blank, then anyone will be able to change the security settings.'))
    ->setDisplayOptions('form', [
    'type' => 'string',
    'weight' => 70,
  ]);
  $fields['user_password'] = BaseFieldDefinition::create('string')
    ->setLabel(t('User password'))
    ->setDescription(new TranslatableMarkup('Optional. If you want to restrict the opening of this PDF to those with a password, enter one here.'))
    ->setDisplayOptions('form', [
    'type' => 'string',
    'weight' => 80,
  ]);
  return $fields;
}