class TermsOfUseForm in Terms of Use 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\terms_of_use\Form\TermsOfUseForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of TermsOfUseForm
1 string reference to 'TermsOfUseForm'
File
- src/
Form/ TermsOfUseForm.php, line 15 - Contains \Drupal\terms_of_use\Form\TermsOfUseForm.
Namespace
Drupal\terms_of_use\FormView source
class TermsOfUseForm extends ConfigFormBase {
/**
* Save node title in validateForm and submitForm save it in the configuration
* @var $node_title
*/
protected $node_title;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'terms_of_use_form';
}
/**
* {@inheritdoc}
*/
// Autocomplete for terms_of_use_node_title doesn't work
// terms_of_use.autocomplete is the routing configuration for the path
// /terms_of_use/autocomplete and called \Drupal\terms_of_use\AutocompleteController
// localizated in src/AutocompletController.php.
// Whe a request for the path /terms_of_use/autocomplete is done, it returns
// a Drupal 404 error
// Try http://base_url/terms_of_use/autocomplete
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('terms_of_use.settings');
// Adding the fieldset for node specification.
$form['terms_of_use_text'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="fieldset-wrapper">',
'#suffix' => '</div>',
);
$form['terms_of_use_text']['terms_of_use_node_id'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Node id where your Terms of Use are published'),
'#default_value' => $config
->get('node_id'),
'#description' => $this
->t('Node <em>id</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
);
/*$form['terms_of_use_text']['terms_of_use_node_title'] = array(
'#type' => 'textfield',
'#title' => $this->t('Title of the post where your Terms of Use are published'),
'#default_value' => $config->get('node_title')),
'#description' => $this->t('Node <em>title</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
'#autocomplete_route_name' => 'terms_of_use.autocomplete',
);
$form['terms_of_use_text']['terms_of_use_pick_node_id'] = array(
'#type' => 'button',
'#value' => $this->t('I prefer to specify the node id'),
'#weight' => 10,
'#ajax' => array(
'callback' => '::js',
'wrapper' => 'fieldset-wrapper',
),
);*/
// Adding the fieldset for form specification.
$form['terms_of_use_form'] = array(
'#type' => 'fieldset',
);
$form['terms_of_use_form']['terms_of_use_fieldset_name'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Label for the fieldset'),
'#default_value' => $config
->get('fieldset_name'),
'#description' => $this
->t('The text for the Terms of Use and the [x] checkbox are contained in a fieldset. Type here the title for that fieldset.'),
);
$form['terms_of_use_form']['terms_of_use_checkbox_label'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Label for the checkbox'),
'#default_value' => $config
->get('checkbox_label'),
'#description' => $this
->t('Type here something like "I agree with these terms." or "I CERTIFY THAT I AM OVER THE AGE OF 18 YEARS OLD.", without quotes. You can use the token @link to insert a link to the Terms in this label. For example, the label can be: "I agree with the @link.", without quotes. You may want to link to the Terms if you prefer not to show the full text of the Terms in the registration form. If you use the token, the Terms will not be shown.'),
);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*
* First check if a nid was set. Then check if the nid exists
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (NULL !== $form_state
->getValue('terms_of_use_node_id')) {
$nid = $form_state
->getValue('terms_of_use_node_id');
if (empty($nid)) {
$form_state
->setErrorByName('terms_of_use_node_id', $this
->t('You must specify a node <em>nid</em>.'));
}
else {
$node = Node::load($nid);
if ($node == NULL) {
$form_state
->setErrorByName('terms_of_use_node_id', $this
->t('No post was published with <em>nid</em> !nid.', array(
'!nid' => $nid,
)));
}
else {
$this->node_title = $node
->label();
}
}
}
/*
elseif (!empty($form_state['values']['terms_of_use_node_title'])) {
$nid = db_select('node', 'n')
->fields('n', array('nid'))
->condition('n.title', db_like($form_state['values']['terms_of_use_node_title']), 'LIKE')
->condition('n.status', 1)
->range(0, 1)
->addTag('node_access')
->execute()
->fetchField();
if (!$nid) {
$form_state->setErrorByName('terms_of_use_node_title', $this->t('No post was published with this title.'));
}
else {
$config->set('node_id', $nid);
}
}
else {
$form_state->setErrorByName('terms_of_use_node_title', $this->t('You must specify a node title.'));
}*/
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this
->config('terms_of_use.settings');
$node_id = $form_state
->getValue('terms_of_use_node_id');
$fieldset_name = $form_state
->getValue('terms_of_use_fieldset_name');
$checkbox_label = $form_state
->getValue('terms_of_use_checkbox_label');
$config
->set('node_id', $node_id)
->set('node_title', $this->node_title)
->set('fieldset_name', $fieldset_name)
->set('checkbox_label', $checkbox_label)
->save();
}
/**
* Menu callback for AHAH addition.
*/
public function js(array &$form, FormStateInterface $form_state) {
$config = $this
->config('terms_of_use.settings');
if (isset($form['terms_of_use_text']['terms_of_use_node_title'])) {
// Create the extra field.
$form['terms_of_use_text']['terms_of_use_node_id'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Node id where your Terms of Use are published'),
'#default_value' => $config
->get('node_id', ''),
'#description' => $this
->t('Node <em>id</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
);
unset($form['terms_of_use_text']['terms_of_use_node_title']);
$form['terms_of_use_text']['terms_of_use_pick_node_id']['#value'] = $this
->t('I prefer to provide the title of the post');
}
else {
// Create the extra field.
$form['terms_of_use_text']['terms_of_use_node_title'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Title of the post where your Terms of Use are published'),
'#default_value' => $config
->get('node_title', ''),
'#description' => t('Node <em>title</em> of the page or story (or blog entry or book page) where your Terms of Use are published.'),
'#autocomplete_route_name' => 'terms_of_use.autocomplete',
);
unset($form['terms_of_use_text']['terms_of_use_node_id']);
$form['terms_of_use_text']['terms_of_use_pick_node_id']['#value'] = $this
->t('I prefer to specify the node id');
}
// Return the form.
// @todo: Resolve the bug when user click node_id button and then cannot return back to node_title field.
return $form['terms_of_use_text'];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
ConfigFormBaseTrait:: |
abstract protected | function | Gets the configuration names that will be editable. | 32 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TermsOfUseForm:: |
protected | property | Save node title in validateForm and submitForm save it in the configuration | |
TermsOfUseForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
TermsOfUseForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
TermsOfUseForm:: |
public | function | Menu callback for AHAH addition. | |
TermsOfUseForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
TermsOfUseForm:: |
public | function |
First check if a nid was set. Then check if the nid exists Overrides FormBase:: |
|
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |