class CclActionsForm in Custom Contextual Links 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\ccl_actions\Form\CclActionsForm
Expanded class hierarchy of CclActionsForm
1 string reference to 'CclActionsForm'
- ccl_actions.routing.yml in ccl_actions/
ccl_actions.routing.yml - ccl_actions/ccl_actions.routing.yml
File
- ccl_actions/
src/ Form/ CclActionsForm.php, line 14 - Contains \Drupal\ccl_actions\Form\CclActionsForm.
Namespace
Drupal\ccl_actions\FormView source
class CclActionsForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ccl_actions_form';
}
public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $clid = NULL) {
// Check if we are in edit mode and load the link values.
if ($clid) {
$link = db_query('SELECT * FROM {ccl} WHERE clid = :clid', [
':clid' => $clid,
])
->fetchObject();
$form_state
->set([
'clid',
], $clid);
// Unserialize options.
$link->options = unserialize($link->options);
$node_options = $link->options['node_options'];
if ($node_options == 'node') {
$title = db_query('SELECT title FROM {node} WHERE nid = :nid', [
':nid' => $link->options['node_id'],
])
->fetchField();
$nid_text = $title . ' [nid:' . $link->options['node_id'] . ']';
}
}
$form = [];
// Pull in library, js and css for the form.
$form['#attached']['library'][] = [
'system',
'ui.button',
];
$form['#attached']['js'][] = drupal_get_path('module', 'ccl') . '/ccl.js';
$form['#attached']['css'][] = drupal_get_path('module', 'ccl') . '/ccl.css';
// Add an ID wrapper so that the JS and CSS from the main module take effect.
$form['#prefix'] = '<div id="ccl-add-form">';
$form['#suffix'] = '</div>';
$form['action_group'] = [
'#type' => 'fieldset',
'#title' => t('Node Actions'),
'#collapsible' => TRUE,
];
$form['action_group']['actions_select'] = [
'#type' => 'checkboxes',
'#title' => t('Core Actions'),
'#description' => t('Select the core actions that should become availabale.'),
'#default_value' => $clid ? $link->options['actions_select'] : [],
'#options' => [
'publish' => t('Publish/unpublish content'),
'sticky' => t('Make content sticky/unsticky'),
'promote' => t('Promote/remove content from front page'),
],
'#required' => TRUE,
];
$form['options_group'] = [
'#type' => 'fieldset',
'#title' => t('Options'),
'#collapsible' => TRUE,
];
$form['options_group']['node_options'] = [
'#type' => 'radios',
'#title' => t('Show link for'),
'#description' => t('Select if this link should be displayed for all nodes, all nodes of a content type or a specific node.'),
'#options' => [
'node' => t('Single node'),
'ct' => t('Content type'),
'global' => t('All nodes'),
],
'#default_value' => isset($node_options) ? $node_options : 'node',
];
// Load the content type names.
$types = node_type_get_names();
$form['options_group']['node_type'] = [
'#type' => 'select',
'#title' => t('Content Type'),
'#description' => t('The content type this link will be displayed for.'),
'#options' => $types,
'#default_value' => $clid ? $link->options['node_type'] : -1,
'#states' => [
'visible' => [
':input[name="node_options"]' => [
'value' => 'ct',
],
],
],
];
$form['options_group']['node_id'] = [
'#type' => 'textfield',
'#title' => t('Node ID'),
'#description' => t('Enter the title of the node or the id of the node this link should be added to.'),
'#size' => 40,
'#maxlength' => 128,
'#default_value' => isset($nid_text) ? $nid_text : '',
'#autocomplete_path' => 'admin/config/user-interface/ccl/autocomplete',
'#states' => [
'visible' => [
':input[name="node_options"]' => [
'value' => 'node',
],
],
],
];
$form['ccl_save_link'] = [
'#type' => 'submit',
'#value' => t('Save Link'),
];
return $form;
}
public function validateForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$values = $form_state
->getValues();
// Check that that we can get the node id and transform it for saving.
if ($values['node_options'] == 'node') {
if (is_numeric($values['node_id'])) {
$form_state
->setValueForElement($form['options_group']['node_id'], $values['node_id']);
}
elseif (preg_match('/\\[nid:[0-9]*\\]/', $values['node_id'], $match)) {
$form_state
->setValueForElement($form['options_group']['node_id'], substr(rtrim($match[0], ']'), 5));
}
else {
$form_state
->setErrorByName('nid', t('Enter a node id or use the autocomplete widget to select an existing node.'));
}
}
}
public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form_state
->setValue([
'ccl_type',
], 'action');
$selected_actions = array_filter($form_state
->getValue([
'actions_select',
]));
$form_state
->setValue([
'ccl_title',
], \Drupal::translation()
->formatPlural(count($selected_actions), 'Action %actions', ' Actions %actions', [
'%actions' => implode(', ', array_keys($selected_actions)),
]));
$form_state
->setValue([
'ccl_link',
], '- n/a -');
module_load_include('inc', 'ccl', 'ccl.admin');
ccl_add_form_submit($form, $form_state);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CclActionsForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
CclActionsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
CclActionsForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
CclActionsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
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 | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
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. | |
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. |