You are here

function metatag_entity_base_field_info in Metatag 8

Implements hook_entity_base_field_info().

File

./metatag.module, line 601
Contains metatag.module.

Code

function metatag_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  $base_table = $entity_type
    ->getBaseTable();
  $canonical_template_exists = $entity_type
    ->hasLinkTemplate('canonical');

  // Certain classes are just not supported.
  $original_class = $entity_type
    ->getOriginalClass();
  $classes_to_skip = [
    'Drupal\\comment\\Entity\\Comment',
  ];

  // If the entity type doesn't have a base table, has no link template then
  // there's no point in supporting it.
  if (!empty($base_table) && $canonical_template_exists && !in_array($original_class, $classes_to_skip)) {
    $fields['metatag'] = BaseFieldDefinition::create('map')
      ->setLabel(t('Metatags (Hidden field for JSON support)'))
      ->setDescription(t('The meta tags for the entity.'))
      ->setClass('\\Drupal\\metatag\\Plugin\\Field\\MetatagEntityFieldItemList')
      ->setComputed(TRUE)
      ->setTranslatable(TRUE)
      ->setTargetEntityTypeId($entity_type
      ->id());
  }
  return $fields;
}