コード例 #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, "");
 }