Exemplo n.º 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;
 }
Exemplo n.º 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);
 }
Exemplo n.º 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;
 }
Exemplo n.º 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), "");
 }
Exemplo n.º 5
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;
 }
Exemplo n.º 6
0
 /**
  * Process the form and options array before random entities are created. The
  * main purpose here is for taxonomy term reference and entity reference
  * fields to create entities that can be attached to fields.
  *
  * @param array $options
  *   Options array.
  */
 protected static function processBeforeCreateRandom(&$options)
 {
     // We need to use "static" here and not "self" since "getFormClass" needs to
     // be called from individual Entity class to get the correct value.
     $formClass = static::getFormClassName();
     // Instantiate the form class.
     $classForm = new $formClass();
     // First get all field instances.
     $field_instances = $classForm->getEntityObject()->getFieldInstances();
     // Iterate over all the field instances and if the field is to be filled,
     // then process it.
     foreach ($field_instances as $field_name => $field_instance) {
         if (Field::isToBeFilled($classForm, $field_name, $options)) {
             // Check if the field is a taxonomy term field or an entity reference field.
             list($field_class, $widget_type) = Field::getFieldClass($classForm, $field_name);
             $field_class::processBeforeCreateRandom($classForm, $field_name, $options);
         }
     }
 }
Exemplo n.º 7
0
 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);
     }
 }
Exemplo n.º 8
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;
 }
Exemplo n.º 9
0
 /**
  * Fill specified field with the provided values.
  *
  * @param string|array $field_name
  *   Field name if it is present at top-level form element. If it is not at
  *   the top-level form element, then provide an array.
  * @param string|int|array $values
  *   Value that needs to be filled.
  *
  * @return Response
  *   Response object.
  */
 public function fillFieldValues($field_name, $values)
 {
     list($field_class, $widget_type) = Field::getFieldClass($this, $field_name);
     if ($field_class) {
         return $field_class::fillValues($this, $field_name, $values);
     }
     $response = $this->getTreeKeys($field_name);
     if (!$response->getSuccess()) {
         return $this->fillValues($field_name, $values);
     }
     return $this->fillValues($response->getVar(), $values);
 }
 /**
  * This function is called after an EntityForm is submitted. Iterate over
  * $termNames variable and see which ones have taxonomy terms. These taxonomy
  * terms are created during form submission. Add these to global $entities
  * object so that they can be deleted later.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  *
  * @return array
  *   An array with 2 values:
  *   (1) $success: TRUE.
  *   (2) $msg: An empty string.
  */
 public static function processAfterSubmit(Form $formObject, $field_name)
 {
     if (self::isCckField($formObject, $field_name)) {
         $field_info = Field::getFieldInfo($field_name);
         $vocabulary = $field_info['settings']['allowed_values'][0]['vocabulary'];
         $class = "RedTest\\entities\\TaxonomyTerm\\" . Utils::makeTitleCase($vocabulary);
         if (sizeof(self::$termNames)) {
             global $entities;
             foreach (self::$termNames as $termName) {
                 $terms = taxonomy_get_term_by_name($termName, $vocabulary);
                 foreach ($terms as $tid => $term) {
                     // We are assuming that there will be only one term with the same
                     // name in a given vocabulary.
                     $entities['taxonomy_term'][$tid] = new $class($tid);
                     unset(self::$termNames[$termName]);
                 }
             }
         }
     }
     return new Response(TRUE, NULL, "");
 }
Exemplo n.º 11
0
 /**
  * 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), "");
 }
Exemplo n.º 12
0
 /**
  * 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));
 }
Exemplo n.º 13
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), "");
 }
Exemplo n.º 14
0
 public function processAfterSubmit()
 {
     // First get all field instances.
     $field_instances = $this->getEntityObject()->getFieldInstances();
     // Iterate over all the field instances and if the field is to be filled,
     // then process it.
     foreach ($field_instances as $field_name => $field_instance) {
         list($field_class, $widget_type) = Field::getFieldClass($this, $field_name);
         $field_class::processAfterSubmit($this, $field_name);
     }
 }
Exemplo n.º 15
0
 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))));
 }
Exemplo n.º 16
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;
 }