You are here

public function DomainPathHelper::alterEntityForm in Domain Path 8

The domain paths form element for the entity form.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

\Drupal\Core\Entity\ContentEntityInterface $entity: Referenced entity.

Return value

array $form Return the modified form array.

File

src/DomainPathHelper.php, line 82

Class

DomainPathHelper

Namespace

Drupal\domain_path

Code

public function alterEntityForm(&$form, FormStateInterface $form_state, $entity) {
  $domains = $this->entityTypeManager
    ->getStorage('domain')
    ->loadMultipleSorted();
  $config = \Drupal::config('domain_path.settings');

  // Just exit if domain paths is not enabled for this entity.
  if (!$this
    ->domainPathsIsEnabled($entity) || !$domains) {
    return $form;
  }

  // Set up our variables.
  $entity_id = $entity
    ->id();
  $langcode = $entity
    ->language()
    ->getId();
  $show_delete = FALSE;
  $default = '';

  // Container for domain path fields
  $form['path']['widget'][0]['domain_path'] = [
    '#tree' => TRUE,
    '#type' => 'details',
    '#title' => $this
      ->t('Domain-specific paths'),
    '#description' => $this
      ->t('Override the default URL alias (above) for individual domains.'),
    // '#group' => 'path_settings',
    '#weight' => 110,
    '#open' => TRUE,
    '#access' => $this->accountManager
      ->hasPermission('edit domain path entity'),
  ];

  // Add an option to delete all domain paths. This is just for convenience
  // so the user doesn't have to manually remove the paths from each domain.
  $form['path']['widget'][0]['domain_path']['domain_path_delete'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Delete domain-specific aliases'),
    '#default_value' => FALSE,
  ];

  // Add a domain path field for each domain.
  foreach ($domains as $domain_id => $domain) {
    $form['path']['widget'][0]['domain_path'][$domain_id] = [
      '#type' => 'container',
    ];

    // Gather the existing domain path.
    $path = FALSE;
    if ($entity_id) {
      $properties = [
        'source' => '/' . $entity
          ->toUrl()
          ->getInternalPath(),
        'language' => $langcode,
        'domain_id' => $domain_id,
      ];
      if ($domain_paths = $this->entityTypeManager
        ->getStorage('domain_path')
        ->loadByProperties($properties)) {
        $path = reset($domain_paths)
          ->get('alias')
          ->getString();
      }
    }

    // We only need to enable the delete checkbox if we have at least one
    // domain path.
    if (!$show_delete && $path) {
      $show_delete = TRUE;
    }
    $label = $domain
      ->label();
    if ($config
      ->get('alias_title') == 'hostname') {
      $label = $domain
        ->getHostname();
    }
    elseif ($config
      ->get('alias_title') == 'url') {
      $label = $domain
        ->getPath();
    }
    if ($this->moduleHandler
      ->moduleExists('domain_path_pathauto')) {

      //See https://git.drupalcode.org/project/pathauto/-/blob/8.x-1.x/src/PathautoWidget.php#L42
      if (isset($form['path']['widget'][0]['pathauto'])) {
        if ($form['path']['widget'][0]['pathauto']['#type'] == 'checkbox') {
          $form['path']['widget'][0]['domain_path'][$domain_id]['pathauto'] = [
            '#type' => 'checkbox',
            '#title' => $this
              ->t('Generate automatic URL alias for @domain', [
              '@domain' => Html::escape(rtrim($domain
                ->getPath(), '/')),
            ]),
            '#default_value' => $form['path']['widget'][0]['pathauto']['#default_value'],
            '#weight' => -1,
          ];
        }
      }
    }
    $form['path']['widget'][0]['domain_path'][$domain_id]['path'] = [
      '#type' => 'textfield',
      '#title' => Html::escape(rtrim($label, '/')),
      '#default_value' => $path ? $path : $default,
      '#access' => $this->accountManager
        ->hasPermission('edit domain path entity'),
      '#states' => [
        'disabled' => [
          'input[name="path[0][domain_path][domain_path_delete]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    if ($this->moduleHandler
      ->moduleExists('domain_path_pathauto')) {
      $form['path']['widget'][0]['domain_path'][$domain_id]['path']['#states'] = [
        'disabled' => [
          [
            'input[name="path[0][domain_path][domain_path_delete]"]' => [
              'checked' => TRUE,
            ],
          ],
          'OR',
          [
            'input[name="path[0][domain_path][' . $domain_id . '][pathauto]"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    if ($config
      ->get('hide_path_alias_ui')) {

      // Hide the default URL alias for better UI
      if (isset($form['path']['widget'][0]['pathauto'])) {
        $form['path']['widget'][0]['pathauto']['#default_value'] = 0;
        $form['path']['widget'][0]['pathauto']['#access'] = FALSE;
      }
      if (isset($form['path']['widget'][0]['alias'])) {
        $form['path']['widget'][0]['alias']['#default_value'] = '';
        $form['path']['widget'][0]['alias']['#access'] = FALSE;
      }
      unset($form['path']['widget'][0]['domain_path']['#description']);
    }

    // If domain settings are on the page for this domain we only show if
    // it's checked. e.g. on the node form, we only show the domain path
    // field for domains we're publishing to
    if (!empty($form['field_domain_access']['widget']['#options'][$domain_id])) {
      $form['path']['widget'][0]['domain_path'][$domain_id]['#states']['invisible']['input[name="field_domain_access[' . $domain_id . ']"]'] = [
        'unchecked' => TRUE,
      ];
      $form['path']['widget'][0]['domain_path'][$domain_id]['#states']['invisible']['input[name="field_domain_all_affiliates[value]"]'] = [
        'unchecked' => TRUE,
      ];
    }
    else {
      if (!empty($form['field_domain_access']['widget']['#options'])) {
        $form['path']['widget'][0]['domain_path'][$domain_id]['#access'] = FALSE;
      }
    }
  }
  $form['path']['widget'][0]['domain_path']['domain_path_delete']['#access'] = $show_delete;

  // Add our validation and submit handlers.
  $form['#validate'][] = [
    $this,
    'validateEntityForm',
  ];
  if (!empty($form['actions'])) {
    if (array_key_exists('submit', $form['actions'])) {
      $form['actions']['submit']['#submit'][] = [
        $this,
        'submitEntityForm',
      ];
    }
  }
  else {

    // If no actions we just tack it on to the form submit handlers.
    $form['#submit'][] = [
      $this,
      'submitEntityForm',
    ];
  }
}