I wrote my own implementation of OutboundPathProcessorInterface
to rewrite the URL of nodes to use the content of their field_project_url field.
namespace Drupal\mymodule\PathProcessor;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\PathProcessor;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\Request;
class MymoduleFieldRedirectPathProcessor implements OutboundPathProcessorInterface {
public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
if (isset($options['entity']) && is_object($options['entity']) && method_exists($options['entity'], 'hasField')) {
$node = $options['entity'];
if ($node->hasField('field_project_url') && !$node->field_project_url->isEmpty()) {
$project_url = $node->field_project_url->getValue();
if (preg_match("/^\/node\/[0-9]+\/(edit|translate|delete|translations)$/i", $path)) {
// dont alter operational paths
}
else {
$path = $project_url[0]['uri'];
dpm($project_url[0]['uri']); // 'http://url.com'
$options['absolute'] = TRUE;
$options['external'] = TRUE;
$options['base_url'] = null;
$options['prefix'] = null;
}
if ($bubbleable_metadata) {
$bubbleable_metadata->addCacheableDependency($node);
}
}
}
return $path;
}
}
The teaser of that view has a title, an image linked to the content, and smart trimmed version of the body field, also linking to the content.
The value is appended, giving (for example) http://localhost:8888/myprojecthttp%3A//url.com.
This used to work, but I can't even get this right with older Drupal 8 versions.
What am I doing wrong?
$options['external']
would work.processOutbound()
should not be expected to redirect to external sites.