View source
<?php
namespace Drupal\google_adwords_path\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
class GoogleAdwordsPathCodeForm extends FormBase {
public function getFormId() {
return 'google_adwords_path_code_form';
}
public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state, $cid = NULL) {
$language = \Drupal::languageManager()
->getCurrentLanguage();
$form = [];
if ($cid) {
$code = google_adwords_path_load_code_by_cid($cid);
$form['cid'] = [
'#type' => 'value',
'#value' => $cid,
];
}
$form['name'] = [
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($code) ? $code['name'] : '',
'#size' => 75,
'#maxlength' => 64,
'#required' => TRUE,
'#description' => t('The name of this conversion code. This will appear in the administrative interface to easily identify it.'),
];
$form['conversion_id'] = [
'#type' => 'textfield',
'#title' => t('Conversion ID'),
'#default_value' => isset($code) ? $code['conversion_id'] : '',
'#size' => 15,
'#maxlength' => 64,
'#required' => TRUE,
];
$form['conversion_label'] = [
'#type' => 'textfield',
'#title' => t('Conversion Label'),
'#default_value' => isset($code) ? $code['conversion_label'] : '',
'#size' => 30,
'#maxlength' => 64,
'#required' => TRUE,
];
$form['paths'] = [
'#type' => 'textarea',
'#title' => t('Paths'),
'#default_value' => isset($code) ? $code['paths'] : '',
'#rows' => 8,
'#cols' => 128,
'#required' => TRUE,
'#description' => t('A list of paths, separated by a new line, where this conversion code should be inserted.'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
];
if ($cid) {
$form['delete'] = [
'#type' => 'submit',
'#value' => t('Delete'),
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state['clicked_button']['#value'] == 'Save') {
$form_state
->cleanValues();
google_adwords_path_save_code($form_state
->getValues());
if (isset($form_state
->getValues()['cid'])) {
$message = 'Successfully updated %name.';
}
else {
$message = 'Successfully added %name.';
}
drupal_set_message(t($message, [
'%name' => $form_state
->getValues()['name'],
]));
$path = 'admin/config/system/google_adwords/path';
$response = new RedirectResponse($path);
$response
->send();
}
elseif ($form_state['clicked_button']['#value'] == 'Delete') {
$path = 'admin/config/system/google_adwords/path/' . $form_state
->getValues()['cid'] . '/delete';
$response = new RedirectResponse($path);
$response
->send();
}
}
}