Score:0

Linkit Module - Pointing the link's title attribute to a custom field. Hook available?

us flag

Currently the Linkit module delivers the link's title attribute by pulling the related node's title field.

We would like it to point to another field in the node.

How can we achieve this?

I looked into Editor Advanced Link module, but I need this to be automatic, and my custom field changes based on content type.

Maybe someone has a hook idea?

Score:3
cn flag

There are (at least) 2 options:

  1. Write a custom filter plugin, basing it on the LinkitFilter plugin included with the Linkit module. In the process() method, where the title is currently set, introduce you own logic to set the title field instead of the existing $entity->label().

    This method has the benefit of auto-updating when a referenced node's title changes, but does mean you'll have to maintain your filter alongside the Linkit module's releases. The process() method does a lot more than just setting the title, and new features/bug fixes in the module will need to be incorporated into your code.

  2. Alter the link dialog form (like Editor Advanced Link does), and get the title into the markup while it's still in the editor. This method is a bit easier, and less maintenance, but the title attribute won't update when the node title does. It will only update when the link dialog form is saved again.

    If that's acceptable, this is a rough but working example to get started:

    use Drupal\Core\Form\FormStateInterface;
    use Drupal\node\NodeInterface;
    
    function custom_module_form_editor_link_dialog_alter(&$form, FormStateInterface $form_state, $form_id) {
      array_unshift($form['#validate'], '_custom_module_link_dialog_validate');
    }
    
    function _custom_module_link_dialog_validate(array &$form, FormStateInterface $form_state) {
      $attributes = $form_state->getValue('attributes');
    
      if (isset($attributes['data-entity-type']) && $attributes['data-entity-type'] == 'node') {
        $node = \Drupal::service('entity.repository')
          ->loadEntityByUuid('node', $attributes['data-entity-uuid']);
        if ($node instanceof NodeInterface) {
          $override_title = '';
          switch ($node->getType()) {
            case 'page':
              $override_title = $node->field_title_override->value;
              break;
            case 'foo':
              // ...
              break;
          }
          if (strlen($override_title)) {
            $form_state->setValue(['attributes', 'title'], $override_title);
          }
        }
      }
    }
    
us flag
Thanks for these!
us flag
Thanks for these! And for editing my question to make it clearer. We would need this to only apply on the front end. And not all content type will have this field available. Is there a hook in the Linkit module that would allow for the filter to check if the node has a certain field existing and filled and replace it on the fly on the front end?
cn flag
No, unfortunately it’s hard coded into the middle of the process method at the moment. You would need to replicate the whole filter for that approach
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.