Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use \Drupal\Core\Form\FormStateInterface;
- use \Drupal\domain\DomainInterface;
- use \Drupal\Core\Render\BubbleableMetadata;
- /**
- * 1) В поле редактирования домена добавляем новое поле
- *
- * Implements hook_form_FORM_ID_alter(): domain_edit_form.
- */
- function subdomains_form_domain_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
- $domain = $form_state->getFormObject()->getEntity(); /** @var DomainInterface $domain */
- $form['additional_settings']['cityname'] = [
- '#type' => 'textfield',
- '#title' => 'Название города',
- '#default_value' => $domain->getThirdPartySetting('subdomains', 'cityname'),
- ];
- $form['#entity_builders'][] = 'subdomains_form_domain_edit_form_entity_builder';
- }
- /**
- * 2) Сохраняем значение нашего поля
- *
- * Domain form entity builder callback.
- */
- function subdomains_form_domain_edit_form_entity_builder($entity_type, DomainInterface $domain, &$form, FormStateInterface $form_state) {
- $domain->setThirdPartySetting('subdomains', 'cityname', $form_state->getValue('cityname'));
- }
- /**
- * 3) Создаём кастомные токены, в них будем подставлять значения, ранее сохранённые в конфиге домена
- *
- * Implements hook_token_info().
- */
- function subdomains_token_info() {
- $info = [];
- $info['types']['subdomains'] = ['name' => t('Subdomains'), 'description' => t('Subdomain tokens - town, phone, coords, etc')];
- $info['tokens']['subdomains']['cityname'][] = 'Name of city, nominative case';
- $info['tokens']['subdomains']['suffix'][] = 'In city';
- $info['tokens']['subdomains']['addr'][] = 'Address of office in current subdomain';
- $info['tokens']['subdomains']['phone'][] = 'Phone number';
- $info['tokens']['subdomains']['coords'][] = "Map's coordinates";
- return $info;
- }
- /**
- * 4) Обработка токенов - если видим "наш", достаём для него значение через коллбэк-функцию
- *
- * Implements hook_tokens().
- */
- function subdomains_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
- $replacements = [];
- if ($type == 'subdomains') {
- foreach ($tokens as $name => $original) {
- // Find the desired token by name.
- switch ($name) {
- case 'cityname':
- $replacements[$original] = subdomains_gettokenval($name);
- break;
- }
- }
- }
- return $replacements;
- }
- /**
- * 5) Коллбэк-функция, получаем имя токена, возвращает соответствующее значение из конфига домена
- *
- * Generate value for a specific token
- */
- function subdomains_gettokenval($name){
- $val = '';
- // для тестирования - будет работать только при ?deb=y
- if (isset($_GET['deb']) && ($_GET['deb'] == 'y')){
- // что писать здесь, как получить настройку из конфига домена?
- //$val = $domain->getThirdPartySetting('subdomains', $name);
- }
- return $val;
- }
Advertisement
Add Comment
Please, Sign In to add comment