Example #1
0
 public static function fillValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, NULL, "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $field_class = get_called_class();
     //$values = $field_class::createInput($values);
     $values = $field_class::formatValuesForCompare($values);
     $response = $formObject->fillMultiValued($field_name, $values);
     $response->setVar($field_class::normalize($response->getVar()));
     return $response;
 }
Example #2
0
 /**
  * Fills text area field with provided values.
  *
  * @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' 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.",
  *       'format' => 'filtered_html',
  *     ),
  *     array(
  *       'value' => "This is text string 2.",
  *       'format' => 'plain_text',
  *     ),
  *   );
  *
  * @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.
  */
 public static function fillValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $defaults = array();
     if (!empty($format)) {
         $defaults['format'] = $format;
     }
     $field_class = get_called_class();
     return $field_class::fillTextValues($formObject, $field_name, $values, $defaults);
 }
Example #3
0
 /**
  * Fills provided decimal values in the field.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param mixed $values
  *   Following formats are acceptable:
  *   (a) 23.67
  *   (b) array(23.34, -89.12)
  *   (c) array(array('value' => 23.34), array('value' => -89.12))
  *
  * @return array
  *   An array with 3 values:
  *   (1) $success: Whether values could be filled in the field.
  *   (2) $values: Values that were filled for the field.
  *   (3) $msg: Message in case there is an error. This will be empty if
  *   $success is TRUE.
  */
 public static function fillValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, NULL, "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $field_class = get_called_class();
     list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
     $decimal_separator = $field['settings']['decimal_separator'];
     $values = $field_class::normalizeInput($values, $decimal_separator);
     $input = $field_class::addCorrectDecimalSeparator($values, $decimal_separator);
     $input = $field_class::formatValuesForInput($input);
     $response = $formObject->fillMultiValued($field_name, $input);
     if (!$response->getSuccess()) {
         $response->normalizeVar();
         return $response;
     }
     $response->setVar(Utils::normalize($values));
     return $response;
 }
Example #4
0
 public static function fillPhoneTextfieldValues(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 = array($values);
     }
     $input = array();
     $index = 0;
     foreach ($values as $key => $value) {
         $input[$index] = array('email' => $value);
         $triggering_element_name = $field_name . '_add_more';
         //$triggering_element_value = 'Add another item';
         $formObject->pressButton($triggering_element_name, array('ajax' => TRUE));
         $index++;
     }
     //$formObject->setValues($field_name, array(LANGUAGE_NONE => $input));
     return new Response(TRUE, Utils::normalize($input), "");
 }
Example #5
0
 public static function fillOptionsSelectValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $formObject->emptyField($field_name);
     if (is_string($values)) {
         $values = array($values);
     }
     $input = array();
     if (sizeof($values)) {
         foreach ($values as $key => $value) {
             if (is_string($value) || is_numeric($value)) {
                 $input[$value] = $value;
             }
         }
         $response = $formObject->fillValues($field_name, array(LANGUAGE_NONE => $input));
     }
     if (isset($response)) {
         $response->setVar($input);
     } else {
         $response = new Response(TRUE, $input, '');
     }
     return $response;
 }
 public static function fillOptionsSelectValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $formObject->emptyField($field_name);
     $vocabulary = NULL;
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         $vocabulary = $field['settings']['allowed_values'][0]['vocabulary'];
     }
     $field_class = get_called_class();
     $tids = $field_class::convertValues($values, $vocabulary);
     $response = $formObject->fillValues($field_name, array(LANGUAGE_NONE => $tids));
     if (!$response->getSuccess()) {
         return $response;
     }
     $termObjects = TaxonomyTerm::createTermObjectsFromTids($tids, $vocabulary);
     $response->setVar($termObjects);
     return $response;
 }
 /**
  * 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), "");
 }
 /**
  * Fills taxonomy term reference checkboxes field of a form with provided
  * values.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param int|object|array $values
  *   An integer taxonomy term id, a term object or an array of tids or term
  *   objects. Here are the acceptable formats:
  *   (a) 23
  *   (b) array(
  *         'tid' => 23,
  *       )
  *   (c) (object) array(
  *         'tid' => 23,
  *       )
  *   (d) (Tag) array(
  *         'entity' => Entity object,
  *         'entity_term' => 'taxonomy_term',
  *       )
  *   (e) array(23, 3)
  *   (f) array(
  *         array(
  *           'tid' => 23,
  *         ),
  *         array(
  *           'tid' => 3,
  *         ),
  *       )
  *   (g) array(
  *         (object) array(
  *           'tid' => 23,
  *         ),
  *         (object) array(
  *           'tid' => 3,
  *         ),
  *       )
  *   (h) array(
  *         (Tag) array(
  *           'entity' => Entity object,
  *           'entity_term' => 'taxonomy_term',
  *         ),
  *         (Tag) array(
  *           'entity' => Entity object,
  *           'entity_term' => 'taxonomy_term',
  *         ),
  *       )
  *
  * @return array
  */
 public static function fillOptionsSelectValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $vocabulary = '';
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
         $vocabulary = $field['settings']['allowed_values'][0]['vocabulary'];
     }
     $tids = array();
     if (is_object($values)) {
         $parent_class = get_parent_class($values);
         if ($parent_class == "RedTest\\core\\entities\\TaxonomyTerm") {
             $tids = array(Utils::getId($values));
         } else {
             $tids = array($values->tid);
         }
     } elseif (is_array($values)) {
         if (array_key_exists('tid', $values)) {
             $tids = array($values['tid']);
         }
         foreach ($values as $key => $value) {
             if (is_numeric($value)) {
                 $tids[] = $value;
             } elseif (is_object($value)) {
                 $parent_class = get_parent_class($value);
                 if ($parent_class == "RedTest\\core\\entities\\TaxonomyTerm") {
                     $tids[] = Utils::getId($value);
                 } else {
                     $tids[] = $value->tid;
                 }
             } elseif (is_array($value)) {
                 $tids[] = $value['tid'];
             }
         }
     }
     $termObjects = array();
     foreach ($tids as $tid) {
         $term_class = "RedTest\\entities\\TaxonomyTerm\\" . Utils::makeTitleCase($vocabulary);
         $termObjects[] = new $term_class($tid);
     }
     return $formObject->fillValues($field_name, array(LANGUAGE_NONE => $tids));
 }
Example #9
0
 /**
  * Upload file.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param string|array $file_info
  *   An image path or an array of image paths relative to Drupal root
  *   folder. The acceptable formats are:
  *   (1) "tests/assets/Filename.jpg"
  *   (2) array(
  *         'uri' => 'Directory1/Filename.jpg",
  *         'description' => 'File description', // this is an optional
  *                                              // parameter,
  *         'scheme' => 'private', // this is an optional parameter
  *       )
  *   (3) array("Directory1/Filename1.jpg", "Directory2/Filename2.jpg")
  *   (4) array(
  *         array(
  *           'uri' => 'Directory1/Filename1.jpg",
  *           'description' => 'File description 1', // this is an optional
  *                                                  // parameter
  *           'scheme' => 'private', // this is an optional parameter
  *         ),
  *         array(
  *           'uri' => 'Directory2/Filename2.jpg",
  *           'description' => 'File description 2', // this is an optional
  *                                                  // parameter
  *           'scheme' => 'public', // this is an optional parameter
  *         ),
  *       )
  *
  * @return mixed $image_paths
  *   A path or an array of paths of images which are to be uploaded.
  */
 public static function fillValues(Form $formObject, $field_name, $file_info)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $field_class = get_called_class();
     $values = $field_class::normalizeInput($file_info);
     list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
     $short_field_class = Utils::makeTitleCase($field['type']);
     $field_class = "RedTest\\core\\fields\\" . $short_field_class;
     $original_values = $formObject->getValues($field_name);
     $original_values = !empty($original_values[LANGUAGE_NONE]) ? $original_values[LANGUAGE_NONE] : array();
     $return = array();
     $input = array();
     for ($i = 0; $i < sizeof($original_values); $i++) {
         if ($original_values[$i]['fid']) {
             $response = $formObject->pressButton($field_name . '_' . LANGUAGE_NONE . '_0_remove_button', array('ajax' => TRUE));
             if (!$response->getSuccess()) {
                 return $response;
             }
         }
     }
     for ($i = 0; $i < sizeof($values); $i++) {
         unset($_SESSION['messages']['error']);
         $file_temp = $field_class::saveFile($values[$i]);
         if (!$file_temp) {
             $errors = $_SESSION['messages']['error'];
             $errors[] = 'Make sure that the destination directory, i.e. sites/default/files, is writable by PHP CLI.';
             $formObject->setErrors($errors);
             return new Response(FALSE, $return, implode(", ", $formObject->getErrors()));
         }
         $input[$i] = $field_class::createInput($file_temp, $values[$i]);
         $return[$i] = $input[$i];
         $return[$i]['uri'] = $file_temp->uri;
         $triggering_element_name = $field_class::getTriggeringElementName($field_name, $i);
         $response = $formObject->fillValues($field_name, array(LANGUAGE_NONE => $input));
         if (!$response->getSuccess()) {
             $response->normalizeVar();
             return $response;
         }
         $response = $formObject->pressButton($triggering_element_name, array('ajax' => TRUE));
         if (!$response->getSuccess()) {
             $response->normalizeVar();
             return $response;
         }
     }
     return new Response(TRUE, Utils::normalize($return), "");
 }
 public static function fillEntityreferenceAutocompleteValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $vocabulary = '';
     if (method_exists($formObject, 'getEntityObject')) {
         // This is an entity form.
         list($field, $instance, $num) = $formObject->getFieldDetails($field_name);
     }
     return $formObject->fillValues($field_name, array(LANGUAGE_NONE => array('0' => array('target_id' => $values))));
 }
Example #11
0
 public static function fillOptionsSelectValues(Form $formObject, $field_name, $values)
 {
     if (!Field::hasFieldAccess($formObject, $field_name)) {
         return new Response(FALSE, "", "Field " . Utils::getLeaf($field_name) . " is not accessible.");
     }
     $formObject->emptyField($field_name);
     if (is_string($values) || is_numeric($values)) {
         $values = array($values);
     }
     $input = array();
     if (sizeof($values)) {
         foreach ($values as $key => $value) {
             if (is_string($value) || is_numeric($value)) {
                 // If value is 0, then make it a string otherwise it will be
                 // interpreted as not selected.
                 $input[$value] = $value === 0 ? strval($value) : $value;
             }
         }
         $response = $formObject->fillValues($field_name, array(LANGUAGE_NONE => $input));
         $output = $response->getVar();
         $input = $output[LANGUAGE_NONE];
     }
     if (isset($response)) {
         $response->setVar($input);
     } else {
         $response = new Response(TRUE, $input, "");
     }
     return $response;
 }