Ejemplo n.º 1
0
 /**
  * Fill form with default values. These default values are what you define in
  * this function and are different from Drupal's default values for the
  * fields.
  *
  * @param array $options
  *   An associative options array. It can have the following keys:
  *   (a) skip: An array of field names which are not to be filled.
  *   (b) required_fields_only: TRUE if only required fields are to be filled
  *   and FALSE if all fields are to be filled.
  *
  * @return array
  *   An array with the following values:
  *   (1) $success: TRUE if fields were filled successfully and FALSE
  *   otherwise.
  *   (2) $fields: An associative array of field values that are to be filled
  *   keyed by field name.
  *   (3) $msg: Error message if $success is FALSE, and an empty string
  *   otherwise.
  */
 public function fillRandomValues($options = array())
 {
     $options += array('skip' => array(), 'required_fields_only' => TRUE);
     $response = parent::fillRandomValues($options);
     if (!$response->getSuccess()) {
         return $response;
     }
     $fields = $response->getVar();
     if (!$options['required_fields_only'] || $this->isDescriptionRequired()) {
         // Check if the field is required. We use '#required' key in form array
         // since it can be set or unset using custom code.
         // Field is required or we need to fill all fields.
         if (!in_array('description', $options['skip'])) {
             $description = array('value' => Utils::getRandomText(100), 'format' => 'plain_text');
             $response = $this->fillDescriptionValues($description);
             if (!$response->getSuccess()) {
                 $response->setVar($fields);
                 return $response;
             }
             $fields['description'] = $description['value'];
             $fields['format'] = $description['format'];
         }
     }
     // Fill name at the end so that there is less chance of getting non-unique
     // value in the database.
     if (!in_array('name', $options['skip'])) {
         // Make sure that taxonomy term name is not repeated so that deleting
         // entities at the end is easier.
         $name = TaxonomyTerm::getUniqueName($this->vocabulary->machine_name);
         $response = $this->fillNameValues($name);
         if (!$response->getSuccess()) {
             $response->setVar($fields);
             return $response;
         }
         $fields['name'] = $name;
     }
     return new Response(TRUE, $fields, "");
 }
 /**
  * Whether the provided value is a term object.
  *
  * @param $value
  * @param $vocabulary
  *
  * @return bool|object
  */
 private static function isTermObject($value, $vocabulary)
 {
     if ($termObject = TaxonomyTerm::termExistsForTid($value, $vocabulary)) {
         // $values follows acceptable format (a).
         return $termObject;
     } elseif ($termObject = TaxonomyTerm::termExistsForName($value, $vocabulary)) {
         // $values follows acceptable format (b).
         return $termObject;
     } elseif (is_object($value)) {
         $parent_class = get_parent_class($value);
         if ($parent_class == "RedTest\\core\\entities\\TaxonomyTerm") {
             // $values follows acceptable format (i).
             return $value;
         } elseif (property_exists($value, 'tid') && ($termObject = TaxonomyTerm::termExistsForTid($value->tid, $vocabulary))) {
             // $values follows acceptable format (f).
             return $termObject;
         } elseif (property_exists($value, 'name') && ($termObject = TaxonomyTerm::termExistsForTid($value->tid, property_exists($value, 'vocabulary') ? $value->vocabulary : $vocabulary))) {
             // $values follows acceptable format (g).
             return $termObject;
         }
     } elseif (is_array($value)) {
         if (array_key_exists('tid', $value) && ($termObject = TaxonomyTerm::termExistsForTid($value['tid'], $vocabulary))) {
             // $values follows acceptable format (c).
             return $termObject;
         } elseif (array_key_exists('name', $value) && ($termObject = TaxonomyTerm::termExistsForName($value['name'], array_key_exists('vocabulary', $value) ? $value['vocabulary'] : $vocabulary))) {
             // $values follows acceptable format (d).
             return $termObject;
         }
     }
 }
Ejemplo n.º 3
0
 public static function createTermObjectsFromNames($names, $vocabulary = NULL, $false_on_invalid = TRUE)
 {
     $termObjects = array();
     foreach ($names as $name) {
         if ($termObject = TaxonomyTerm::termExistsForName($name, $vocabulary)) {
             $termObjects[] = $termObject;
         } elseif (!$false_on_invalid) {
             $termObjects[] = $name;
         } else {
             $termObjects[] = FALSE;
         }
     }
     return $termObjects;
 }