コード例 #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 Response
  *   Response object.
  */
 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();
     // @todo Check about field access.
     if ((!$options['required_fields_only'] || $this->isTitleRequired()) && !in_array('title', $options['skip'])) {
         // 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 not required. There is no need to fill this field.
         // @todo Provide the ability to pass max_length in $options array.
         $response = $this->fillTitleRandomValues();
         if (!$response->getSuccess()) {
             $response->setVar($fields);
             return $response;
         }
         $fields['title'] = $response->getVar();
     }
     // @todo Check in $skip array.
     if ($this->hasFieldAccess(array('options', 'status')) && isset($options['status'])) {
         $status = NULL;
         switch ($options['status']) {
             case 'random':
                 $status = Utils::getRandomBool();
                 break;
             case 'published':
                 $status = 1;
                 break;
             case 'unpublished':
                 $status = 0;
                 break;
         }
         if (!is_null($status)) {
             $response = $this->fillStatusValues($status);
             if (!$response->getSuccess()) {
                 $response->setVar($fields);
                 return $response;
             }
             $fields['status'] = $status;
         }
     }
     // @todo Check in $skip array.
     if ($this->hasFieldAccess(array('revision_information', 'revision')) && isset($options['revision'])) {
         $revision = NULL;
         $revision_log = NULL;
         switch ($options['revision']) {
             case 'random':
                 $revision = Utils::getRandomBool();
                 break;
             case TRUE:
                 $revision = 1;
                 break;
             case FALSE:
                 $revision = 0;
                 break;
         }
         if (!is_null($revision)) {
             $response = $this->fillRevisionValues($revision);
             if (!$response->getSuccess()) {
                 $response->setVar($fields);
                 return $response;
             }
             $fields['revision'] = $revision;
             if ($revision && $this->hasFieldAccess(array('revision_information', 'log')) && isset($options['revision_log'])) {
                 switch ($options['revision_log']) {
                     case 'random':
                         $revision_log = Utils::getRandomBool() ? Utils::getRandomText(25) : NULL;
                         break;
                     case TRUE:
                         $revision_log = Utils::getRandomText(25);
                         break;
                     case FALSE:
                         $revision_log = '';
                         break;
                 }
                 if (!is_null($revision_log)) {
                     $response = $this->fillLogValues($revision_log);
                     if (!$response->getSuccess()) {
                         $response->setVar($fields);
                         return $response;
                     }
                     $fields['log'] = $revision_log;
                 }
             }
         }
     }
     // @todo Check $skip array.
     if ($this->hasFieldAccess(array('author', 'name')) && isset($options['change_author']) && $options['change_author']) {
         // We'll need to create new author first.
         // Masquerade as user 1.
         list($superAdmin, $originalUser, $originalState) = User::masquerade(1);
         $response = User::createRandom();
         // Return to original user.
         User::unmasquerade($originalUser, $originalState);
         if (!$response->getSuccess()) {
             $response->setVar($fields);
             return $response;
         }
         $userObject = $response->getVar();
         $name = $userObject->getNameValues();
         $this->fillNameValues($name);
         $fields['name'] = $name;
     }
     // @todo Check $skip array.
     if ($this->hasFieldAccess(array('author', 'date')) && isset($options['change_published_date']) && $options['change_published_date']) {
         $now = time();
         $start = isset($options['start_published_date']) ? Utils::formatDate($options['start_published_date'], 'integer') : $now - 3 * 365 * 24 * 60 * 60;
         $end = isset($options['end_published_date']) ? Utils::formatDate($options['end_published_date'], 'integer') : $now + 3 * 365 * 24 * 60 * 60;
         $date = Utils::getRandomDate('Y-m-d H:i:s', $start, $end);
         $response = $this->fillDateValues($date);
         if (!$response->getSuccess()) {
             $response->setVar($fields);
             return $response;
         }
         $fields['date'] = $date;
     }
     return new Response(TRUE, $fields, "");
 }
コード例 #2
0
 /**
  * Create taxonomy terms before a new entity form is invoked.
  *
  * @param Form $formObject
  *   Form object.
  * @param string $field_name
  *   Field name.
  * @param array $options
  *   Options array. "references" key is used here.
  *
  * @return array
  *   An array with two values:
  *   (1) $success: Whether the function executed successfully.
  *   (2) $msg: A message if $success is FALSE.
  */
 public static function processBeforeCreateRandom(Form $formObject, $field_name, &$options)
 {
     $field_info = self::getFieldInfo($field_name);
     $vocabulary = $field_info['settings']['allowed_values'][0]['vocabulary'];
     $cardinality = $field_info['cardinality'];
     $num = 1;
     if ($cardinality == -1) {
         // -1 denotes that cardinality is unlimited.
         $num = 5;
     } else {
         $num = intval($cardinality);
     }
     // Check if $references has these terms already.
     $options += array('references' => array());
     $references =& $options['references'];
     if (isset($references['taxonomy_terms'][$vocabulary])) {
         if (sizeof($references['taxonomy_terms'][$vocabulary]) < $num) {
             $num -= sizeof($references['taxonomy_terms'][$vocabulary]);
         } else {
             $num = 0;
         }
     } else {
         $references['taxonomy_terms'][$vocabulary] = array();
     }
     if ($num) {
         $base_path = "RedTest\\entities\\TaxonomyTerm\\";
         $class = $base_path . Utils::makeTitleCase($vocabulary);
         // Masquerade as user 1 so that there is no access problem.
         list($superUserObject, $userObject, $old_state) = User::masquerade(1);
         $response = $class::createRandom($num);
         if (!$response->getSuccess()) {
             return new Response(FALSE, NULL, "Could not create terms of vocabulary {$vocabulary} attached to {$field_name}: " . $response->getMsg());
         }
         User::unmasquerade($userObject, $old_state);
         $termObjects = $response->getVar();
         if (is_object($termObjects)) {
             $termObjects = array($termObjects);
         }
         $references['taxonomy_terms'][$vocabulary] = array_merge($references['taxonomy_terms'][$vocabulary], $termObjects);
     }
     return new Response(TRUE, NULL, "");
 }