public function CclActionsForm::buildForm in Custom Contextual Links 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- ccl_actions/
src/ Form/ CclActionsForm.php, line 23 - Contains \Drupal\ccl_actions\Form\CclActionsForm.
Class
Namespace
Drupal\ccl_actions\FormCode
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;
}