public static function getValues(Entity $entityObject, $field_name, $post_process = TRUE) { list($field, $instance, $num) = Field::getFieldDetails($entityObject, $field_name); $short_field_class = Utils::makeTitleCase($field['type']); $field_class = "RedTest\\core\\Fields\\" . $short_field_class; $widget_type = Utils::makeTitleCase($instance['widget']['type']); $function = "get" . $widget_type . "Values"; if (method_exists($field_class, $function)) { return $field_class::$function($entityObject, $field_name); } else { return $entityObject->getFieldItems($field_name); } }
/** * Get value of a field. * * @param string $field_name * Field name. * @param bool $post_process * Whether to post process the field values before returning. * * @return mixed $output * Value of the field. * * @throws \EntityMalformedException */ public function getFieldValues($field_name, $post_process = TRUE) { list($field, $instance, $num) = Field::getFieldDetails($this, $field_name); if (!is_null($field) && !is_null($instance)) { // Field instance exists. $short_field_class = Utils::makeTitleCase($field['type']); $field_class = "RedTest\\core\\Fields\\" . $short_field_class; return $field_class::getValues($this, $field_name, $post_process); } // There is no such field instance for the given entity. Check if it's a // property. if (isset($this->entity->{$field_name})) { return $this->entity->{$field_name}; } return NULL; }
/** * Fills the field of the form with provided values. * * @param Form $formObject * Form object. * @param string $field_name * Field name. * @param string|array $values * Following are the acceptable formats: * (a) "0", "1" * (b) array(0, 1) * * @return string|array * Values that are filled in the field. Following are the returned formats: * (a) 0, 1 * (b) array(0, 1) * If multiple values are filled, then the return variable will be in * format (b), otherwise it will be in format (a). */ public static function fillOptionsOnOffValues(Form $formObject, $field_name, $values) { if (!Field::hasFieldAccess($formObject, $field_name)) { return new Response(FALSE, NULL, "Field " . Utils::getLeaf($field_name) . " is not accessible."); } $formObject->emptyField($field_name); if (is_string($values) || is_numeric($values)) { // $values is in acceptable format (a). $values = array($values); } $response = NULL; $input = array(); $output = array(); if (sizeof($values)) { if (sizeof($values) == 1 && ($values[0] === 0 || $values[0] === "0")) { if (method_exists($formObject, 'getEntityObject')) { $entityObject = $formObject->getEntityObject(); list($field, $instance, $num) = Field::getFieldDetails($entityObject, $field_name); //if ($num == 1) { // This is a single checkbox and value input is 0 so set it to NULL. $input = NULL; $output[0] = 0; /*} else { // This is a multi-valued field so 0 is a key. Set it to be a string. $input[0] = "0"; $output[0] = 0; }*/ } } else { foreach ($values as $key => $value) { if (is_string($value) || is_numeric($value)) { // $values is in acceptable format (b). $output[] = $value; $input[$value] = $value === 0 ? strval($value) : $value; } } } $response = $formObject->fillValues($field_name, array(LANGUAGE_NONE => $input)); if (!$response->getSuccess()) { $response->setVar(Utils::normalize($output)); return $response; } } return new Response(TRUE, Utils::normalize($output), ""); }