Example #1
0
 /**
  * Merges the provides values with defaults, formats the values into a proper
  * array that can be set in form_state, sets the values in form_state and
  * returns.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param string|array $values
  *   Either a string or an array. If it's a string, then it is assumed that
  *   the field has only one value. If it is an array of strings, then it is
  *   assumed that the field is multi-valued and the strings in the array
  *   correspond to multiple text values of this field. If it is an array of
  *   arrays, then it is assumed that the field is multi-valued and the inside
  *   array can have the keys 'value', 'summary' or 'format' which will be set
  *   in form_state. Here are a few examples this parameter can take:
  *   "<p>This is text string.</p>", or
  *   array("<p>This is text string 1.</p>", "This is text string 2."), or
  *   array(
  *     array(
  *       'value' => "This is text string 1.",
  *       'summary' => "<p>Text string 1</p>",
  *       'format' => 'filtered_html',
  *     ),
  *     array(
  *       'value' => "This is text string 2.",
  *       'summary' => "Text string 2",
  *       'format' => 'plain_text',
  *     ),
  *   );
  * @param array $defaults
  *   Array of defaults.
  * @param int $offset
  *   Offset that is to be passed to fillMultiValued() function.
  *
  * @return array
  *   An array with 3 values:
  *   (1) $success: Whether the field could be filled with provided values.
  *   (2) $values: Values that were filled.
  *   (3) $msg: Error message if $success is FALSE and empty otherwise.
  */
 protected static function fillTextValues(Form $formObject, $field_name, $values, $defaults, $offset = 0)
 {
     $field_class = get_called_class();
     $values = $field_class::convertValuesToInput($values, $defaults);
     if (Field::isCckField($formObject, $field_name)) {
         $response = $formObject->fillMultiValued($field_name, $values, $offset);
     } else {
         $values = is_array($values) ? $values[0]['value'] : $values;
         $response = $formObject->fillValues($field_name, $values);
     }
     $response->normalizeVar();
     return $response;
 }