You are here

public static function LinkCheckerLink::baseFieldDefinitions in Link checker 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/LinkCheckerLink.php, line 253

Class

LinkCheckerLink
Defines the linkcheckerlink type entity.

Namespace

Drupal\linkchecker\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

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

  // Standard field for making each link entity unique.
  $fields[$entity_type
    ->getKey('id')]
    ->setLabel(new TranslatableMarkup('Link ID'))
    ->setDescription(new TranslatableMarkup('The ID of the link entity.'));
  $fields[$entity_type
    ->getKey('published')]
    ->setLabel(new TranslatableMarkup('Check link status'));

  // Hash of URL.
  $fields['urlhash'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('URL hash'))
    ->setDescription(new TranslatableMarkup('The indexable hash of the {linkchecker_link}.url.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', 64);

  // URI related to the link.
  $fields['url'] = BaseFieldDefinition::create('uri')
    ->setLabel(new TranslatableMarkup('URL'))
    ->setDescription(new TranslatableMarkup('The full qualified link.'))
    ->setRequired(TRUE);

  // Method related to the link.
  $fields['method'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Method'))
    ->setDescription(new TranslatableMarkup('The method for checking links (HEAD, GET, POST).'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', 4)
    ->setDefaultValue('HEAD');

  // Code related to the link.
  $fields['code'] = BaseFieldDefinition::create('integer')
    ->setLabel(new TranslatableMarkup('Status code'))
    ->setDescription(new TranslatableMarkup('HTTP status code from link checking.'));

  // Error related to the link.
  $fields['error'] = BaseFieldDefinition::create('string_long')
    ->setLabel(new TranslatableMarkup('Error'))
    ->setDescription(new TranslatableMarkup('The error message received from the remote server while doing link checking.'));

  // Fail count: number of failed checks related to the link of the entity.
  $fields['fail_count'] = BaseFieldDefinition::create('integer')
    ->setLabel(new TranslatableMarkup('Fail count'))
    ->setDescription(new TranslatableMarkup('Fail count of unsuccessful link checks. No flapping detection. (Successful = 0, Unsuccessful = fail_count+1).'))
    ->setSetting('unsigned', TRUE)
    ->setDefaultValue(0);

  // Timestamp for last check related to the link.
  $fields['last_check'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(new TranslatableMarkup('Last checked'))
    ->setDescription(new TranslatableMarkup('Timestamp of the last link check.'))
    ->setDefaultValue(0);

  // Entity id related to the link.
  $fields['entity_id'] = BaseFieldDefinition::create('dynamic_entity_reference')
    ->setLabel(new TranslatableMarkup('Entity id'))
    ->setDescription(new TranslatableMarkup('ID of entity in which link was found.'))
    ->setRequired(TRUE);

  // Entity field related to the link.
  $fields['entity_field'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Entity field'))
    ->setDescription(new TranslatableMarkup('Field of entity in which link was found.'))
    ->setRequired(TRUE);

  // Entity langcode related to the link.
  $fields['entity_langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(new TranslatableMarkup('Entity language'))
    ->setDescription(new TranslatableMarkup('Language of entity in which link was found.'))
    ->setRequired(TRUE);
  return $fields;
}