Score:0

Custom field formatter with blob, value always empty

lc flag

I have the following custom field:

/**
 * Plugin implementation of the 'price_table' field type.
 *
 * @FieldType(
 *   id = "price_table",
 *   default_widget = "price_table_widget",
 *   default_formatter = "price_table_formatter",
 * )
 */
class PriceTable extends FieldItemBase {

  public static function mainPropertyName(): string {
    return 'value';
  }

  public static function schema(FieldStorageDefinitionInterface $field_definition): array {
    return [
      'columns' => [
        'value' => [
          'type' => 'blob',
          'size' => 'big',
          'serialize' => TRUE,
        ],
      ],
    ];
  }

  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition): array {
    $properties['value'] = MapDataDefinition::create()
      ->setLabel(t('Table data'));

    return $properties;
  }

  public function setValue($values, $notify = TRUE) {
    if (isset($values) && !empty($values['table'])) {
      $values['value'] = $values['table'];
      unset($values['table']);
      parent::setValue($values, $notify);
    }
  }

  public function isEmpty(): bool {
    try {
      $value = $this->get('value')->getValue();
      dpm($value); //<<<
      return !is_array($value) || empty($value);
    } catch (MissingDataException | InvalidArgumentException $e) {
      return FALSE;
    }
  }

}

with the appropriate widget (using a table form element) and formatter present. The editing goes fine, the serialized data gets stored into the database record of my entity. However, when it comes to the formatter, it displays nothing because isEmpty() above always gets an empty value and returns FALSE. The blob gets stored all right (thanks to setValue() above) but it isn't retrieved by the field.

What am I missing here?

Score:0
lc flag

Never mind... :-)

public function isEmpty(): bool {
  try {
    $value = $this->getValue();

That simple.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.