Problem/Motivation
Hi, I've set up a plugin as shown on Dialogue on how to implement a 4.0 plugin - with partial code
This is the contents of my plugin
<?php
namespace Drupal\computed_field_sale_badge\Plugin\ComputedField;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\computed_field\Field\ComputedFieldDefinitionWithValuePluginInterface;
use Drupal\computed_field\Plugin\ComputedField\ComputedFieldBase;
/**
* @ComputedField(
* id = "computed_field_sale_badge_sale_badge",
* label = @Translation("Sale Badge"),
* field_type = "text",
* )
*/
class SaleBadge extends ComputedFieldBase {
/**
* {@inheritdoc}
*/
public function computeValue(EntityInterface $host_entity, ComputedFieldDefinitionWithValuePluginInterface $computed_field_definition): array {
$value = '';
if ($host_entity->hasField('list_price') && !$host_entity->get('list_price')->isEmpty()) {
$listprice = $host_entity->get('list_price')->first()->toPrice();
$price = $host_entity->get('price')->first()->toPrice();
$saletext = t('Sale');
if ($listprice > $price) {
$value = '<div class="onsale">' . $saletext . '</div>';
}
}
return [
0 => [
'value' => $value,
'format' => 'full_html',
],
];
}
}
Its a calculated field to show a badge on products that are on sale based on the product variation price. This worked fine in older versions of the module and does work as expected with version 4 following the above thread.
However I'm also getting the error
Warning: Undefined array key "target_type" in Drupal\computed_field\Entity\ComputedField->getSetting() (line 38 of modules/contrib/computed_field/src/Field/ComputedFieldSettingsTrait.php)
Can anybody shed some light on this?
Kind regards,
Steps to reproduce
Create and install custom plugin and enable error logging.
Using Drupal 10.1.5 and Drupal commerce 8.x-2.36
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork computed_field-3393564
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
shreya_th commentedComment #4
shreya_th commentedHi @joachim,
I have fixed this issue and also created MR for this issue i.e. Undefined array key "target_type". Kindly review the changes .
Comment #5
mellowtothemax commentedThanks this fixed it.
Comment #6
paulrad commentedHi, @mellowtothemax!
If @Shreya_th resolved your issue, consider moving it to RTBC status.
Comment #7
mellowtothemax commentedComment #8
joachim commentedCould you provide a backtrace to see who is calling this?
A text type field shouldn't be asked for this setting!
Comment #9
marcusx commentedHave the same error on a commerce product variant. The backtrace looks like this:
Comment #10
joachim commentedThanks!
The code is this:
And WHY is this asking for a setting before it knows the type of the field???
Comment #11
joachim commentedLook like consuming code like Commerce is relying on this UNDOCUMENTED behaviour in FieldStorageConfig:
So we should mimic that and get it documented in core: #3395748: getSetting()'s documentation should specify what happens when a setting doesn't exist.
Comment #12
joachim commentedComment #13
joachim commentedFixed with a slightly simpler fix, but crediting @Shreya_th for the fix & everyone else for helping figure it out and review.
Thanks everyone!