You are here

public function FaqFieldItem::setValue in FAQ Field 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldType/FaqFieldItem.php \Drupal\faqfield\Plugin\Field\FieldType\FaqFieldItem::setValue()

Overrides \Drupal\Core\TypedData\TypedData::setValue().

Parameters

array|null $values: An array of property values.

Overrides FieldItemBase::setValue

File

src/Plugin/Field/FieldType/FaqFieldItem.php, line 121

Class

FaqFieldItem
Plugin implementation of the 'faqfield' field type.

Namespace

Drupal\faqfield\Plugin\Field\FieldType

Code

public function setValue($values, $notify = TRUE) {
  if (is_array($values) && isset($values['answer']) && is_array($values['answer'])) {

    // Normal textarea's and textfields put their values simply in by
    // array($name => $value); Unfortunately text_format textareas put
    // them into an array so also the format gets saved: array($name
    // => array('value' => $value, 'format' => $format)).
    // So the API will try to save normal textfields to the 'name' field
    // and text_format fields to 'answer_value' and 'answer_format'.
    // To bypass this, we pull the values out of this array and force
    // them to be saved in 'answer' and 'answer_format'.
    $values['answer_format'] = $values['answer']['format'];
    $values['answer'] = $values['answer']['value'];
  }
  parent::setValue($values, $notify);
}