Smart Link Entity

As a field, Smart Link content could be attached to an entity. However, I found this limiting for a few reasons.

First, I'd like the ability to have a custom form and other things specific to creating, editing, and managing Smart Links.

Creating an article to add a Smart Link field can be streamlined, but I want something more direct. If I want to attach it to another piece of content, I can just create a reference.

One of the issues I ran into with making a custom field is that I needed to define how it rendered. I wanted each item to render with the field theme hook to have a consistent look as other fields, but that became a hassle, requiring defining many variables that a field needs.

By implementing Smart Link as an entity, each field can be rearranged or formatted separately by the site builder.

Drupal add smart link
Smart link example - how opcache works

I used drush gen content:entity to scaffold the necessary files. To get started, I only had to customize the SmartLink::baseFieldProperties() function.

<?php

namespace Drupal\smart_link\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\smart_link\SmartLinkInterface;
use Drupal\user\UserInterface;

/**
 * Defines the smart link entity class.
 *
 * @ContentEntityType(
 *   id = "smart_link",
 *   label = @Translation("Smart Link"),
 *   label_collection = @Translation("Smart Links"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\smart_link\SmartLinkListBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "access" = "Drupal\smart_link\SmartLinkAccessControlHandler",
 *     "form" = {
 *       "add" = "Drupal\smart_link\Form\SmartLinkForm",
 *       "edit" = "Drupal\smart_link\Form\SmartLinkForm",
 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
 *     }
 *   },
 *   base_table = "smart_link",
 *   admin_permission = "administer smart link",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "title",
 *     "uuid" = "uuid"
 *   },
 *   links = {
 *     "add-form" = "/admin/content/smart-link/add",
 *     "canonical" = "/smart_link/{smart_link}",
 *     "edit-form" = "/admin/content/smart-link/{smart_link}/edit",
 *     "delete-form" = "/admin/content/smart-link/{smart_link}/delete",
 *     "collection" = "/admin/content/smart-link"
 *   },
 *   field_ui_base_route = "entity.smart_link.settings"
 * )
 */
class SmartLink extends ContentEntityBase implements SmartLinkInterface {

  use EntityChangedTrait;

  public function preSave(EntityStorageInterface $storage) {
    $smart_link_info = \Drupal::service('smart_link')->getUrlInfo($this->url->value);
    if (!empty($smart_link_info)) {
      foreach ($smart_link_info as $key => $value) {
        $this->$key = $value;
      }
    }
  }

  /**
   * {@inheritdoc}
   *
   * When a new smart link entity is created, set the uid entity reference to
   * the current user as the creator of the entity.
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    parent::preCreate($storage_controller, $values);
    $values += ['uid' => \Drupal::currentUser()->id()];
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this->get('title')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setTitle($title) {
    $this->set('title', $title);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled() {
    return (bool) $this->get('status')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setStatus($status) {
    $this->set('status', $status);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    return $this->get('created')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $this->set('created', $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwner() {
    return $this->get('uid')->entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getOwnerId() {
    return $this->get('uid')->target_id;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwnerId($uid) {
    $this->set('uid', $uid);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setOwner(UserInterface $account) {
    $this->set('uid', $account->id());
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['url'] = BaseFieldDefinition::create('string')
      ->setLabel(t('URL'))
      ->setDescription(t('The url of the smart link entity.'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -10,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['scheme'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Scheme'))
      ->setDescription(t('The scheme of the smart link entity.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['host'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Host'))
      ->setDescription(t('The host of the smart link entity.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['path'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Path'))
      ->setDescription(t('The path of the smart link entity.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['query'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Query'))
      ->setDescription(t('The query of the smart link entity.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['title'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Title'))
      ->setDescription(t('The title of the smart link entity.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['h1'] = BaseFieldDefinition::create('string')
      ->setLabel(t('H1'))
      ->setDescription(t('The first H1 of the smart link entity.'))
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
        'type' => 'string',
        'label' => 'above',
        'weight' => -5,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['description'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Description'))
      ->setDescription(t('A description of the smart link.'))
      ->setDisplayOptions('view', [
        'type' => 'string',
        'label' => 'above',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['meta'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Meta'))
      ->setDescription(t('The meta tags of the smart link entity.'))
      ->setDisplayOptions('view', [
        'type' => 'string',
        'label' => 'above',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['status'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Status'))
      ->setDescription(t('A boolean indicating whether the smart link is enabled.'))
      ->setDefaultValue(TRUE)
      ->setSetting('on_label', 'Enabled')
      ->setDisplayOptions('form', [
        'type' => 'boolean_checkbox',
        'settings' => [
          'display_label' => FALSE,
        ],
        'weight' => 10,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Author'))
      ->setDescription(t('The user ID of the smart link author.'))
      ->setSetting('target_type', 'user')
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'author',
        'weight' => 15,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Authored on'))
      ->setDescription(t('The time that the smart link was created.'))
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'timestamp',
        'weight' => 20,
      ])
      ->setDisplayConfigurable('view', TRUE);

    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the smart link was last edited.'));

    return $fields;
  }

}