- Issue created by @wxman
- Status changed to Fixed
9 months ago 7:41am 11 March 2024 - π¬π§United Kingdom joachim
You need to do what it tells you and return at least an empty array.
Also, BTW, I think there's a mistake in your code because there's no point setting $ISBN = ''; if you're then going to ignore an empty $ISBN.
- πΊπΈUnited States wxman
You were correct about the mistake. Leftovers from other try's. If you want to see something scary, I asked Bing's Copilot: "how can I improve this PHP code". I pasted my original plugin code into it, and not only did it fix/improve it, it recognized that it was for linking to Amazon Associates. It came back with this:
class Computedbuyamazon extends ComputedFieldBase { /** * {@inheritdoc} */ public function computeValue(EntityInterface $host_entity, ComputedFieldDefinitionWithValuePluginInterface $computed_field_definition): array { $ISBN = $this->getISBN($host_entity); if (!empty($ISBN)) { $value = $this->generateAmazonLink($ISBN); } else { $value = ''; } return [ 0 => [ 'value' => $value, 'format' => 'full_html', ], ]; } /** * Get the ISBN from either field_isbn10 or field_amazon_asin. * * @param EntityInterface $host_entity * @return string|null */ private function getISBN(EntityInterface $host_entity): ?string { if (!empty($host_entity->field_isbn10->value)) { return $host_entity->field_isbn10->value; } elseif (!empty($host_entity->field_amazon_asin->value)) { return $host_entity->field_amazon_asin->value; } return null; } /** * Generate the Amazon link and image HTML. * * @param string $ISBN * @return string */ private function generateAmazonLink(string $ISBN): string { $link = '<a href="https://www.amazon.com/gp/product/' . $ISBN . '/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=' . $ISBN . '&linkCode=as2&tag=thelittim04-20" target="_blank">Amazon.com</a>'; $image = '<img src="https://www.assoc-amazon.com/e/ir?t=thelittim04-20&l=as2&o=1&a=' . $ISBN . '" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />'; return $link . $image; } }
It works perfectly. The AI even gave me pointers and explained why it suggested what it did. Scary!
- π¬π§United Kingdom joachim
When there is no ISBN you should return an empty value, not an array with a 0 delta and empty in that.
Otherwise, your field will output a label with no content, and your Twig templates will think they have a value to work with.
- πΊπΈUnited States wxman
@joachim Okay this is working as shown, so which are you saying needs to be changed?
- π¬π§United Kingdom joachim
I'm minded to close any issues that mention AI anyway, on account of its obscene carbon footprint and shady practices with IP, but if you're questioning my advice over the regurgitated rubbish from fancy autocomplete, I am definitely out of this issue.
- πΊπΈUnited States wxman
@joachim I'm not questioning your advice at all. You are much better at working on this that I will ever be. I was just confused as to which value needs to be changed. I thought if the value comes up empty the view just doesn't show it.
Automatically closed - issue fixed for 2 weeks with no activity.