Repository URL to install this package:
|
Version:
1.48.1 ▾
|
<?php
namespace Drupal\customeredit\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\customeredit\CustomerText;
use Drupal\customeredit\CustomerTextsStorage;
use Drupal\stofa\CartValidateEvent;
use Drupal\stofa\Service\EnvVars;
use Drupal\stofa\Service\Stofa;
use Drupal\stofa\StofaEntities;
use Drupal\stofa_api_sows\Plugin\StofaAPI\Sows\SowsAPI;
use Drupal\stofa_tv\OffersBuilder;
use Drupal\stofa_tv\TVEntities;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EditTextForm extends FormBase {
/**
* @var CustomerTextsStorage
*/
protected $storage;
public static function create(ContainerInterface $container) {
$storage = $container->get('customeredit.texts.storage');
return new static($storage);
}
public function __construct($storage) {
$this->storage = $storage;
}
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'customeredit_edit_form';
}
/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $machine_name
*
* @return array The form structure.
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state, $machine_name = '') {
$text = new CustomerText($machine_name);
$config = $this->config('customeredit.texts.settings');
$form = [
'#title' => $this->t('Edit %label', ['%label' => $text->getTitle()])
];
$form['title'] = [
'#type' => 'item',
'#title' => $text->getTitle(),
'#description' => $text->getDescription(),
];
$form['machine_name'] = [
'#type' => 'hidden',
'#value' => $machine_name,
];
$form['list'] = array(
'#type' => 'table',
// TableSelect: Injects a first column containing the selection widget into
// each table row.
// Note that you also need to set #tableselect on each form submit button
// that relies on non-empty selection values (see below).
'#tableselect' => FALSE,
);
// Build the table rows and columns.
// The first nested level in the render array forms the table row, on which you
// likely want to set #attributes and #weight.
// Each child element on the second level represents a table column cell in the
// respective table row, which are render elements on their own. For single
// output elements, use the table cell itself for the render element. If a cell
// should contain multiple elements, simply use nested sub-keys to build the
// render element structure for drupal_render() as you would everywhere else.
$n = 0;
switch($text->getType()) {
case 'textarea':
$form['list'][$n]['text'] = [
'#type' => 'textarea',
'#default_value' => $text->getText(),
];
break;
case 'text_format':
$form['list'][$n]['text'] = [
'#type' => 'text_format',
'#allowed_formats' => [$config->get('text_format') ?: "full_html"],
'#default_value' => $text->getText(),
];
break;
case 'external-link':
$form['list'][$n]['text'] = [
'#type' => 'url',
'#options' => ['external' => TRUE],
'#default_value' => $text->getText(),
];
break;
case 'button':
case 'link':
$form['list'][$n]['title'] = [
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $text->getValue('title'),
];
$form['list'][$n]['url'] = [
'#type' => 'textfield',
'#title' => t('URL'),
'#default_value' => $text->getValue('url'),
];
break;
default:
$form['list'][$n]['text'] = [
'#type' => 'textfield',
'#default_value' => $text->getText(),
];
break;
}
if (\Drupal::moduleHandler()->moduleExists('token')) {
if ($text->getType() != 'button' && $text->getType() != 'link') {
$form['list'][$n]['text']['#element_validate'] = ['token_element_validate'];
$form['list'][$n]['text']['#token_types'] = $text->getTokenTypes();
} else {
$form['list'][$n]['title']['#element_validate'] = ['token_element_validate'];
$form['list'][$n]['title']['#token_types'] = $text->getTokenTypes();
$form['list'][$n]['url']['#element_validate'] = ['token_element_validate'];
$form['list'][$n]['url']['#token_types'] = $text->getTokenTypes();
}
$form['token_help'] = array(
'#theme' => 'token_tree_link',
'#token_types' => $text->getTokenTypes(),
);
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
);
return $form;
}
/**
* Form validation handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$machine_name = $form_state->getValue('machine_name');
$text = new CustomerText($machine_name);
switch($text->getType()) {
case 'url':
if ($form_state->getValue('text') == '') {
} elseif (customeredit_textToUrl($form_state->getValue('text')) === FALSE) {
$form_state->setErrorByName('text', $this->t('Invalid input'));
}
break;
case 'button':
case 'link':
if ($form_state->getValue('url') == '') {
} elseif (customeredit_textToUrl($form_state->getValue('url')) === FALSE) {
$form_state->setErrorByName('url', $this->t('Invalid input'));
}
default:
}
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$machine_name = $form_state->getValue('machine_name');
$text = new CustomerText($machine_name);
foreach ($form_state->getValue('list') as $item) {
switch ($text->getType()) {
case 'button':
case 'link':
if (is_string($item['title'])) {
$text->setValue('title', $item['title']);
}
elseif (is_array($item['title'])) {
$text->setValue('title', $item['title']['value']);
}
if (is_string($item['url'])) {
$text->setValue('url', $item['url']);
}
elseif (is_array($item['url'])) {
$text->setValue('url', $item['url']['value']);
}
break;
default:
if (is_string($item['text'])) {
$text->setText($item['text']);
}
elseif (is_array($item['text'])) {
$text->setText($item['text']['value']);
}
break;
}
$text->save();
if ($text->mustClearCache()) {
\Drupal::service('cache_tags.invalidator')->invalidateAll();
}
}
$categories = $this->storage->getCategories();
if ($text->getCategory() == $categories[0]) {
$form_state->setRedirect('customertexts.list');
} else {
$transliteration = \Drupal::service('transliteration');
$trans = strtolower(str_replace(' ', '_', $transliteration->transliterate($text->getCategory())));
$form_state->setRedirect('customertexts.list.category', ['category_id' => $trans]);
}
}
}