Guest User

Untitled

a guest
Jun 8th, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. <?php
  2.  
  3. use \Drupal\Core\Form\FormStateInterface;
  4. use \Drupal\domain\DomainInterface;
  5. use \Drupal\Core\Render\BubbleableMetadata;
  6.  
  7. /**
  8.  * 1) В поле редактирования домена добавляем новое поле
  9.  *
  10.  * Implements hook_form_FORM_ID_alter(): domain_edit_form.
  11.  */
  12. function subdomains_form_domain_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  13.   $domain = $form_state->getFormObject()->getEntity(); /** @var DomainInterface $domain */
  14.  
  15.   $form['additional_settings']['cityname'] = [
  16.     '#type' => 'textfield',
  17.     '#title' => 'Название города',
  18.     '#default_value' => $domain->getThirdPartySetting('subdomains', 'cityname'),
  19.   ];
  20.  
  21.   $form['#entity_builders'][] = 'subdomains_form_domain_edit_form_entity_builder';
  22. }
  23.  
  24. /**
  25.  * 2) Сохраняем значение нашего поля
  26.  *
  27.  * Domain form entity builder callback.
  28.  */
  29. function subdomains_form_domain_edit_form_entity_builder($entity_type, DomainInterface $domain, &$form, FormStateInterface $form_state) {
  30.   $domain->setThirdPartySetting('subdomains', 'cityname', $form_state->getValue('cityname'));
  31. }
  32.  
  33. /**
  34.  * 3) Создаём кастомные токены, в них будем подставлять значения, ранее сохранённые в конфиге домена
  35.  *
  36.  * Implements hook_token_info().
  37.  */
  38. function subdomains_token_info() {
  39.   $info = [];
  40.   $info['types']['subdomains'] = ['name' => t('Subdomains'), 'description' => t('Subdomain tokens - town, phone, coords, etc')];
  41.   $info['tokens']['subdomains']['cityname'][] = 'Name of city, nominative case';
  42.   $info['tokens']['subdomains']['suffix'][] = 'In city';
  43.   $info['tokens']['subdomains']['addr'][] = 'Address of office in current subdomain';
  44.   $info['tokens']['subdomains']['phone'][] = 'Phone number';
  45.   $info['tokens']['subdomains']['coords'][] = "Map's coordinates";
  46.   return $info;
  47. }
  48.  
  49. /**
  50.  * 4) Обработка токенов - если видим "наш", достаём для него значение через коллбэк-функцию
  51.  *
  52.  * Implements hook_tokens().
  53.  */
  54. function subdomains_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  55.   $replacements = [];
  56.   if ($type == 'subdomains') {
  57.  
  58.     foreach ($tokens as $name => $original) {
  59.       // Find the desired token by name.
  60.       switch ($name) {
  61.         case 'cityname':
  62.           $replacements[$original] = subdomains_gettokenval($name);
  63.           break;
  64.       }
  65.     }
  66.   }
  67.   return $replacements;
  68. }
  69.  
  70. /**
  71.  * 5) Коллбэк-функция, получаем имя токена, возвращает соответствующее значение из конфига домена
  72.  *
  73.  * Generate value for a specific token
  74.  */
  75. function subdomains_gettokenval($name){
  76.  
  77.     $val = '';
  78.  
  79.     // для тестирования - будет работать только при ?deb=y
  80.     if (isset($_GET['deb']) && ($_GET['deb'] == 'y')){
  81.         // что писать здесь, как получить настройку из конфига домена?
  82.         //$val = $domain->getThirdPartySetting('subdomains', $name);
  83.     }
  84.  
  85.     return $val;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment