domain_menu_access.module 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * @file
  4. * Domain-based access control for menu link.
  5. */
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\Core\Form\FormStateInterface;
  8. /**
  9. * Implements hook_ENTITY_TYPE_presave().
  10. *
  11. * Fires only if Devel Generate module is present, to assign test menu
  12. * link content to domains.
  13. */
  14. function domain_menu_access_menu_link_content_presave(EntityInterface $node) {
  15. domain_access_presave_generate($node);
  16. }
  17. /**
  18. * Implements hook_form_BASE_FORM_ID_alter() for
  19. * \Drupal\menu_link_content\MenuLinkContentForm.
  20. *
  21. * Move Domain Access fields to an advanced tab like other node settings.
  22. */
  23. function domain_menu_access_form_menu_link_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  24. /** @var \Drupal\menu_link_content\Form\MenuLinkContentForm $form_object */
  25. $form_object = $form_state->getFormObject();
  26. /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $entity */
  27. $entity = $form_object->getEntity();
  28. $config = \Drupal::config('domain_menu_access.settings')->get('menu_enabled');
  29. if (!empty($config) && in_array($entity->getMenuName(), $config)) {
  30. $form['domain'] = [
  31. '#type' => 'details',
  32. '#title' => t('Domain'),
  33. '#open' => TRUE,
  34. '#weight' => 25,
  35. ];
  36. $form[DOMAIN_ACCESS_FIELD]['#group'] = 'domain';
  37. $form[DOMAIN_ACCESS_ALL_FIELD]['#group'] = 'domain';
  38. // Add the options hidden from the user silently to the form.
  39. $manager = \Drupal::service('domain.element_manager');
  40. $form = $manager->setFormOptions($form, $form_state, DOMAIN_ACCESS_FIELD);
  41. }
  42. else {
  43. $form[DOMAIN_ACCESS_FIELD]['#access'] = FALSE;
  44. $form[DOMAIN_ACCESS_ALL_FIELD]['#access'] = FALSE;
  45. }
  46. }