Problem/Motivation

We have a need on several projects for a shared field_short_title field, with a [node:shorter-title] token. It would be nice to have this provided in Pipewrench since it's opinionated and would be good to be available as an API to use in other code instead of having to duplicate it.

  /**
   * Gets the short title or label for an entity.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The node entity.
   *
   * @return string|null
   *   The short title, or entity label, or NULL neither were avilable.
   */
  public static function getEntityShortLabel(EntityInterface $entity): ?string {
    return $entity->hasField('field_short_title') && !$entity->get('field_short_title')->isEmpty() ? $entity->get('field_short_title')->getString() : $entity->label();
  }

Issue fork pipewrench-3575558

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dave reid created an issue. See original summary.

dave reid’s picture

Title: Create service for getting entity field_short_title or label (and token integration) » Entity short title/label with token and menu_ui integration
marcoscano’s picture

My suggestion:
- New submodule: pipewrench_short_title
- The module ships with a new ShortTitleField field type plugin that extends \Drupal\Core\Field\Plugin\Field\FieldType\StringItem and just overrides the getter and the ::isEmpty() to fallback to the entity label if the stored value is empty.
- When the module is installed, a new basefield is added to node entities, using our field type
- (Possible follow-up): Make it configurable to add the base field to other entities too
- A third-party setting on bundles to make this basefield "active" for that particular bundle. Or maybe we could assume that site-builders would know how to activate it by adjusting form/view displays?

I actually almost think this could make sense in its own project, but I see https://www.drupal.org/project/short_title is taken with a slightly different feature-set, so maybe OK to have it bundled here? 🤷🏼

dave reid’s picture

Yeah, I like the idea of having it as a separate field type, because overriding the getter and isEmpty was making sense to me, it would help simplify the logic and not need a custom token anymore.

Sigh, I wish core had a unified way of adding third party settings to entity bundle config entities, but it doesn't.