0

I have a link field in a content type, field_source. I created a computed field (field_source_domain) in the same content type, which should contain the domain for the link in field_source.

I tried with $value = parse_url('https://subdomain.example.com/foo/bar', PHP_URL_HOST); and it works, but when I tried $value = parse_url('$entity->field_source->value', PHP_URL_HOST); or $value = parse_url('$fields['field_source']', PHP_URL_HOST);, I didn't get anything.

How do I get the domain name using a computed field?

4
  • Can you inspect the output of $entity->field_source->value and $fields['field_source'] does it return the full url https://subdomain.example.com/foo/bar or something different? like an array or relative url? – Rick B Mar 31 '20 at 18:14
  • It doesn't return anything. I think this is the problem. I can't get the url from these variables and i can't find the right variable that returns the correct value of the field_source. – Theodore Dimitriadis Apr 1 '20 at 6:14
  • Ok, then you first have to see how to retreive the correct value, have a look at this: drupal.stackexchange.com/questions/144947/… Also the parse_url function only accepts an absolute url so starting whit https://yourdomain and not a relative one. – Rick B Apr 1 '20 at 7:55
  • I tried that but i got a WSOD, i think because the link field has multiple values, title and url. I want only the url and i don't know how to get it. – Theodore Dimitriadis Apr 1 '20 at 8:16
0

I found the solution to my problem:

$field_source = $entity->get('field_source')->getValue();
$url_link = $field_source[0]['uri'];
$parse = parse_url($url_link);
$value = $parse['host'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.