Conditional Custom Text Filter plugin
That's not so easy, because the filter is normally not aware from which field it is called from.

The token filter module does this trick:

/**
 * Implements hook_preprocess_HOOK().
 */
function token_filter_preprocess_field(&amp;$variables) {
  if (isset($variables['element']['#object']) &amp;&amp; $variables['element']['#object'] instanceof ContentEntityInterface) {
    $entity = &amp;drupal_static('token_filter_entity');
    $entity = $variables['element']['#object'];
  }
}


The parent entity of the field is stored in a static variable and can be used in the filter:

  public function process($text, $langcode) {
    $entity = drupal_static('token_filter_entity', NULL);
    if (!is_null($entity) &amp;&amp; $entity instanceof ContentEntityInterface) {
      // use entity
    }
  }


http://cgit.drupalcode.org/token_filter/tree/token_filter.module?h=8.x-1.x

This may not be the best solution for most cases. If possible look for a better place for the code where you have direct access to the entity. For example in hook_entity_view_alter().