public function checkTaxonomyTermReferenceItems($field_name, $testClass, $values) { $testClass->assertEquals(Utils::getId($values), call_user_func(array($this, "get" . Utils::makeTitleCase($field_name))), "Values of " . $field_name . " do not match."); }
/** * Register a new user. * * @param string $username * Username. * @param string $email * Email address. * @param string $password * Password. * @param array $roles * An array of roles that are to be added to the user in addition to the * default role(s) that the user gets on registering. You can either pass * in role id or role string. * * @return mixed $user * User object if the user logged in successfully and an array of errors, * otherwise. */ public static function registerUser($username, $email, $password, $roles = array()) { $userRegisterForm = new UserForms\UserRegisterForm(); $userRegisterForm->fillFieldValues(array('account', 'name'), $username); $userRegisterForm->fillFieldValues(array('account', 'mail'), $email); $userRegisterForm->fillFieldValues(array('pass', 'pass1'), $password); $userRegisterForm->fillFieldValues(array('pass', 'pass2'), $password); $response = $userRegisterForm->submit(); if (!$response->getSuccess()) { return $response; } /** * @todo Find a better way to make the user active and add roles than using user_save(). */ $roles = self::formatRoles($roles); $userObject = $response->getVar(); if (!$userObject->getStatusValues() || sizeof($roles)) { $account = $userObject->getEntity(); $edit['status'] = TRUE; $edit['roles'] = $account->roles + $roles; $account = user_save($account, $edit); if (!$account) { return new Response(FALSE, NULL, "Could not make the user active or could not add roles."); } $userObject = new User(Utils::getId($userObject)); } // Add password key so that it can be used later to log in. $form_state = $userRegisterForm->getFormState(); $account = $userObject->getEntity(); $account->password = $form_state['user']->password; $userObject->setEntity($account); return new Response(TRUE, $userObject, ""); }
/** * 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)); }
/** * Register a new user. * * @param string $username * Username. * @param string $email * Email address. * @param string $password * Password. * @param array $options * Options array. Usually this will have the following 3 keys: * (a) roles: An array of roles that the newsly created user has to be * assigned. * (b) skip: An array of fields that need to be skipped during * registration. * (c) required_fields_only: Whether only required fields in the * registration field are to be filled. * * @return mixed $user * User object if the user logged in successfully and an array of errors, * otherwise. */ public static function registerUser($username, $email, $password, $options = array()) { $options += array('roles' => array(), 'skip' => array(), 'required_fields_only' => TRUE); $userRegisterForm = new UserForms\UserRegisterForm(); $userRegisterForm->fillFieldValues(array('account', 'name'), $username); $userRegisterForm->fillFieldValues(array('account', 'mail'), $email); $userRegisterForm->fillFieldValues(array('pass', 'pass1'), $password); $userRegisterForm->fillFieldValues(array('pass', 'pass2'), $password); $userObject = new User(); $field_instances = $userObject->getFieldInstances(); $fields = array(); foreach ($field_instances as $field_name => $field_info) { if (!$field_info['settings']['user_register_form']) { continue; } if (!in_array($field_name, $options['skip']) && ($userRegisterForm->isRequired($field_name) || $field_info['required'] || !$options['required_fields_only'])) { $function = 'fill' . Utils::makeTitleCase($field_name) . 'RandomValues'; $response = $userRegisterForm->{$function}(); if (!$response->getSuccess()) { $response->setVar($fields); return $response; } $fields[$field_name] = $response->getVar(); } } $response = $userRegisterForm->submit(); if (!$response->getSuccess()) { return $response; } /** * @todo Find a better way to make the user active and add roles than using user_save(). */ try { $roles = self::formatRoles($options['roles']); } catch (\Exception $e) { return new Response(FALSE, NULL, $e->getMessage()); } $userObject = $response->getVar(); if (!$userObject->getStatusValues() || sizeof($roles)) { $account = $userObject->getEntity(); $edit['status'] = TRUE; $edit['roles'] = $account->roles + $roles; $account = user_save($account, $edit); if (!$account) { return new Response(FALSE, NULL, "Could not make the user active or could not add roles."); } $userObject = new User(Utils::getId($userObject)); } // Add password key so that it can be used later to log in. $form_state = $userRegisterForm->getFormState(); $account = $userObject->getEntity(); $account->password = $form_state['user']->password; $userObject->setEntity($account); return new Response(TRUE, $userObject, ""); }