You are here

public static function SavedSearch::baseFieldDefinitions in Search API Saved Searches 8

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/SavedSearch.php, line 79

Class

SavedSearch
Provides an entity type for saved searches.

Namespace

Drupal\search_api_saved_searches\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  $fields = parent::baseFieldDefinitions($entity_type);

  // Make the form display of the language configurable.
  $fields['langcode']
    ->setDisplayConfigurable('form', TRUE);
  $fields['label'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Label'))
    ->setDescription(t('The label for the saved search.'))
    ->setRequired(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -5,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Created by'))
    ->setDescription(t('The user who owns the saved search.'))
    ->setSetting('target_type', 'user')
    ->setDefaultValueCallback('Drupal\\search_api_saved_searches\\Entity\\SavedSearch::getCurrentUserId')
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'author',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Activated'))
    ->setDescription(t('Whether the saved search has been activated.'))
    ->setDefaultValue(TRUE)
    ->setDisplayOptions('view', [
    'type' => 'boolean',
    'weight' => 0,
    'settings' => [
      'on_label' => t('Activated'),
      'off_label' => t('Activation pending'),
    ],
  ])
    ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created on'))
    ->setDescription(t('The time that the saved search was created.'))
    ->setDisplayOptions('view', [
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 10,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['last_executed'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Last executed'))
    ->setDescription(t('The time that the saved search was last checked for new results.'))
    ->setDisplayOptions('view', [
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 10,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['next_execution'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Next execution'))
    ->setDescription(t('The next time this saved search should be executed.'))
    ->setDisplayOptions('view', [
    'type' => 'timestamp',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'datetime_timestamp',
    'weight' => 10,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['notify_interval'] = BaseFieldDefinition::create('list_integer')
    ->setLabel(t('Notification interval'))
    ->setDescription(t('The interval in which you want to receive notifications of new results for this saved search.'))
    ->setRequired(TRUE)
    ->setSetting('allowed_values', [
    3600 => t('Hourly'),
    86400 => t('Daily'),
    604800 => t('Weekly'),
    -1 => t('Never'),
  ])
    ->setDefaultValue(-1)
    ->setDisplayOptions('view', [
    'type' => 'list_default',
    'weight' => 0,
  ])
    ->setDisplayOptions('form', [
    'type' => 'options_select',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['index_id'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Index ID'))
    ->setSetting('max_length', 50);

  // @todo Is there a better data type? Should we provide one?
  $fields['query'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Search query'))
    ->setDescription(t('The saved search query.'))
    ->setSetting('case_sensitive', TRUE)
    ->setDisplayOptions('view', [
    'region' => 'hidden',
  ])
    ->setDisplayOptions('form', [
    'region' => 'hidden',
  ]);
  $fields['path'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Path'))
    ->setDescription(t("The path to this saved search's query."))
    ->setSetting('case_sensitive', TRUE)
    ->setDisplayOptions('view', [
    'region' => 'hidden',
  ])
    ->setDisplayOptions('form', [
    'region' => 'hidden',
  ])
    ->setDisplayConfigurable('form', TRUE);
  return $fields;
}