You are here

class OgMenuInstanceForm in Organic Groups Menu (OG Menu) 8

Form controller for OG Menu instance edit forms.

Hierarchy

Expanded class hierarchy of OgMenuInstanceForm

File

src/Form/OgMenuInstanceForm.php, line 31

Namespace

Drupal\og_menu\Form
View source
class OgMenuInstanceForm extends ContentEntityForm {

  /**
   * The menu link manager.
   *
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $menuLinkManager;

  /**
   * The menu tree service.
   *
   * @var \Drupal\Core\Menu\MenuLinkTreeInterface
   */
  protected $menuTree;

  /**
   * The link generator.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $linkGenerator;

  /**
   * The OG access service.
   *
   * @var \Drupal\og\OgAccessInterface $og_access
   */
  protected $ogAccess;

  /**
   * The overview tree form.
   *
   * @var array
   */
  protected $overviewTreeForm = array(
    '#tree' => TRUE,
  );

  /**
   * The entity being used by this form.
   *
   * @var \Drupal\og_menu\OgMenuInstanceInterface
   */
  protected $entity;

  /**
   * The URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * Constructs an OgMenuInstanceForm.
   *
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository service.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
   *   The menu link manager.
   * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
   *   The menu tree service.
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
   *   The link generator.
   * @param \Drupal\og\OgAccessInterface $og_access
   *   The OG Access service.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The URL generator.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface\null $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface\null $time
   *   The time service.
   */
  public function __construct(EntityRepositoryInterface $entity_repository, MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, LinkGeneratorInterface $link_generator, OgAccessInterface $og_access, UrlGeneratorInterface $url_generator, ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, ?TimeInterface $time = NULL) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->menuLinkManager = $menu_link_manager;
    $this->menuTree = $menu_tree;
    $this->linkGenerator = $link_generator;
    $this->ogAccess = $og_access;
    $this->urlGenerator = $url_generator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.repository'), $container
      ->get('plugin.manager.menu.link'), $container
      ->get('menu.link_tree'), $container
      ->get('link_generator'), $container
      ->get('og.access'), $container
      ->get('url_generator'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('datetime.time'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    // On entity add, no links are attached yet, so bail out here.
    if ($this->entity
      ->isNew()) {
      return $form;
    }

    // Ensure that menu_overview_form_submit() knows the parents of this form
    // section.
    if (!$form_state
      ->has('menu_overview_form_parents')) {
      $form_state
        ->set('menu_overview_form_parents', []);
    }
    $form['#attached']['library'][] = 'menu_ui/drupal.menu_ui.adminforms';
    $tree = $this->menuTree
      ->load('ogmenu-' . $this->entity
      ->id(), new MenuTreeParameters());

    // We indicate that a menu administrator is running the menu access check.
    $this
      ->getRequest()->attributes
      ->set('_menu_admin', TRUE);
    $manipulators = array(
      array(
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ),
      array(
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ),
    );
    $tree = $this->menuTree
      ->transform($tree, $manipulators);
    $this
      ->getRequest()->attributes
      ->set('_menu_admin', FALSE);

    // Determine the delta; the number of weights to be made available.
    $count = function (array $tree) {
      $sum = function ($carry, MenuLinkTreeElement $item) {
        return $carry + $item
          ->count();
      };
      return array_reduce($tree, $sum);
    };
    $delta = max($count($tree), 50);
    $form['links'] = array(
      '#type' => 'table',
      '#theme' => 'table__menu_overview',
      '#header' => array(
        $this
          ->t('Menu link'),
        array(
          'data' => $this
            ->t('Enabled'),
          'class' => array(
            'checkbox',
          ),
        ),
        $this
          ->t('Weight'),
        array(
          'data' => $this
            ->t('Operations'),
          'colspan' => 3,
        ),
      ),
      '#attributes' => array(
        'id' => 'menu-overview',
      ),
      '#tabledrag' => array(
        array(
          'action' => 'match',
          'relationship' => 'parent',
          'group' => 'menu-parent',
          'subgroup' => 'menu-parent',
          'source' => 'menu-id',
          'hidden' => TRUE,
          'limit' => \Drupal::menuTree()
            ->maxDepth() - 1,
        ),
        array(
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'menu-weight',
        ),
      ),
    );

    // Check if the user has the global permission to add new links to the menu
    // instance, or has this permission inside the group.
    $permission = 'add new links to og menu instance entities';
    $has_permission = $this
      ->currentUser()
      ->hasPermission($permission) || $this->ogAccess
      ->userAccess($this->entity
      ->getGroup(), $permission, $this
      ->currentUser(), FALSE, TRUE)
      ->isAllowed();

    // Supply the empty text.
    if ($has_permission) {
      $form['links']['#empty'] = $this
        ->t('There are no menu links yet. <a href=":url">Add link</a>.', [
        ':url' => $this->urlGenerator
          ->generateFromRoute('entity.ogmenu_instance.add_link', [
          'ogmenu_instance' => $this->entity
            ->id(),
        ], [
          'query' => [
            'destination' => $this->entity
              ->toUrl('edit-form')
              ->toString(),
          ],
        ]),
      ]);
    }
    else {
      $form['links']['#empty'] = $this
        ->t('There are no menu links yet.');
    }
    $links = $this
      ->buildOverviewTreeForm($tree, $delta);
    foreach (Element::children($links) as $id) {
      if (isset($links[$id]['#item'])) {
        $element = $links[$id];
        $form['links'][$id]['#item'] = $element['#item'];

        // TableDrag: Mark the table row as draggable.
        $form['links'][$id]['#attributes'] = $element['#attributes'];
        $form['links'][$id]['#attributes']['class'][] = 'draggable';

        // TableDrag: Sort the table row according to its existing/configured weight.
        $form['links'][$id]['#weight'] = $element['#item']->link
          ->getWeight();

        // Add special classes to be used for tabledrag.js.
        $element['parent']['#attributes']['class'] = array(
          'menu-parent',
        );
        $element['weight']['#attributes']['class'] = array(
          'menu-weight',
        );
        $element['id']['#attributes']['class'] = array(
          'menu-id',
        );
        $form['links'][$id]['title'] = array(
          array(
            '#theme' => 'indentation',
            '#size' => $element['#item']->depth - 1,
          ),
          $element['title'],
        );
        $form['links'][$id]['enabled'] = $element['enabled'];
        $form['links'][$id]['enabled']['#wrapper_attributes']['class'] = array(
          'checkbox',
          'menu-enabled',
        );
        $form['links'][$id]['weight'] = $element['weight'];

        // Operations (dropbutton) column.
        $form['links'][$id]['operations'] = $element['operations'];
        $form['links'][$id]['id'] = $element['id'];
        $form['links'][$id]['parent'] = $element['parent'];
      }
    }
    return $form;
  }
  protected function buildOverviewTreeForm($tree, $delta) {
    $form =& $this->overviewTreeForm;
    $tree_access_cacheability = new CacheableMetadata();
    foreach ($tree as $element) {
      $tree_access_cacheability = $tree_access_cacheability
        ->merge(CacheableMetadata::createFromObject($element->access));

      // Only render accessible links.
      if (!$element->access
        ->isAllowed()) {
        continue;
      }

      /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
      $link = $element->link;
      if ($link) {
        $id = 'menu_plugin_id:' . $link
          ->getPluginId();
        $form[$id]['#item'] = $element;
        $form[$id]['#attributes'] = $link
          ->isEnabled() ? array(
          'class' => array(
            'menu-enabled',
          ),
        ) : array(
          'class' => array(
            'menu-disabled',
          ),
        );
        $form[$id]['title'] = Link::fromTextAndUrl($link
          ->getTitle(), $link
          ->getUrlObject())
          ->toRenderable();
        if (!$link
          ->isEnabled()) {
          $form[$id]['title']['#suffix'] = ' (' . $this
            ->t('disabled') . ')';
        }
        elseif ($id === 'menu_plugin_id:user.logout') {
          $form[$id]['title']['#suffix'] = ' (' . $this
            ->t('<q>Log in</q> for anonymous users') . ')';
        }
        elseif (($url = $link
          ->getUrlObject()) && $url
          ->isRouted() && $url
          ->getRouteName() == 'user.page') {
          $form[$id]['title']['#suffix'] = ' (' . $this
            ->t('logged in users only') . ')';
        }
        $form[$id]['enabled'] = array(
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Enable @title menu link', array(
            '@title' => $link
              ->getTitle(),
          )),
          '#title_display' => 'invisible',
          '#default_value' => $link
            ->isEnabled(),
        );
        $form[$id]['weight'] = array(
          '#type' => 'weight',
          '#delta' => $delta,
          '#default_value' => $link
            ->getWeight(),
          '#title' => $this
            ->t('Weight for @title', array(
            '@title' => $link
              ->getTitle(),
          )),
          '#title_display' => 'invisible',
        );
        $form[$id]['id'] = array(
          '#type' => 'hidden',
          '#value' => $link
            ->getPluginId(),
        );
        $form[$id]['parent'] = array(
          '#type' => 'hidden',
          '#default_value' => $link
            ->getParent(),
        );

        // Build a list of operations.
        $operations = array();
        $operations['edit'] = array(
          'title' => $this
            ->t('Edit'),
        );

        // Allow for a custom edit link per plugin.
        $edit_route = $link
          ->getEditRoute();
        if ($edit_route) {
          $operations['edit']['url'] = $edit_route;

          // Bring the user back to the menu overview.
          $operations['edit']['query'] = $this
            ->getDestinationArray();
        }
        else {

          // Fall back to the standard edit link.
          $operations['edit'] += array(
            'url' => Url::fromRoute('menu_ui.link_edit', [
              'menu_link_plugin' => $link
                ->getPluginId(),
            ]),
          );
        }

        // Links can either be reset or deleted, not both.
        if ($link
          ->isResettable()) {
          $operations['reset'] = array(
            'title' => $this
              ->t('Reset'),
            'url' => Url::fromRoute('menu_ui.link_reset', [
              'menu_link_plugin' => $link
                ->getPluginId(),
            ]),
          );
        }
        elseif ($delete_link = $link
          ->getDeleteRoute()) {
          $operations['delete']['url'] = $delete_link;
          $operations['delete']['query'] = $this
            ->getDestinationArray();
          $operations['delete']['title'] = $this
            ->t('Delete');
        }
        if ($link
          ->isTranslatable()) {
          $operations['translate'] = array(
            'title' => $this
              ->t('Translate'),
            'url' => $link
              ->getTranslateRoute(),
          );
        }

        // Only display the operations to which the user has access.
        foreach ($operations as $key => $operation) {
          if (!$operation['url']
            ->access()) {
            unset($operations[$key]);
          }
        }
        $form[$id]['operations'] = [
          '#type' => 'operations',
          '#links' => $operations,
        ];
      }
      if ($element->subtree) {
        $this
          ->buildOverviewTreeForm($element->subtree, $delta);
      }
    }
    $tree_access_cacheability
      ->merge(CacheableMetadata::createFromRenderArray($form))
      ->applyTo($form);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $menu = $this->entity;
    if (!$menu
      ->isNew() || $menu
      ->isLocked()) {
      $this
        ->submitOverviewForm($form, $form_state);
    }
    $status = $menu
      ->save();
    switch ($status) {
      case SAVED_NEW:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Created the %label menu.', [
          '%label' => $menu
            ->label(),
        ]));
        break;
      default:
        $this
          ->messenger()
          ->addMessage($this
          ->t('Saved the %label menu.', [
          '%label' => $menu
            ->label(),
        ]));
    }
    $field_storage = FieldStorageConfig::loadByName($this->entity
      ->getEntityTypeId(), OgGroupAudienceHelperInterface::DEFAULT_FIELD);
    $target_type = $field_storage
      ->getSetting('target_type');
    $group_entity = $this->entity
      ->getGroup();

    // If possible, redirect to the group after save.
    if ($target_type && $group_entity) {
      $form_state
        ->setRedirect('entity.' . $target_type . '.canonical', [
        $target_type => $group_entity
          ->id(),
      ]);
    }
    else {
      $form_state
        ->setRedirect('entity.ogmenu_instance.edit_form', [
        'ogmenu_instance' => $menu
          ->id(),
      ]);
    }
  }

  /**
   * Submit handler for the menu overview form.
   *
   * This function takes great care in saving parent items first, then items
   * underneath them. Saving items in the incorrect order can break the tree.
   */
  protected function submitOverviewForm(array $complete_form, FormStateInterface $form_state) {

    // Form API supports constructing and validating self-contained sections
    // within forms, but does not allow to handle the form section's submission
    // equally separated yet. Therefore, we use a $form_state key to point to
    // the parents of the form section.
    $parents = $form_state
      ->get('menu_overview_form_parents');
    $input = NestedArray::getValue($form_state
      ->getUserInput(), $parents);
    $form =& NestedArray::getValue($complete_form, $parents);

    // When dealing with saving menu items, the order in which these items are
    // saved is critical. If a changed child item is saved before its parent,
    // the child item could be saved with an invalid path past its immediate
    // parent. To prevent this, save items in the form in the same order they
    // are sent, ensuring parents are saved first, then their children.
    // See https://www.drupal.org/node/181126#comment-632270.
    $order = is_array($input) ? array_flip(array_keys($input)) : array();

    // Update our original form with the new order.
    $form = array_intersect_key(array_merge($order, $form), $form);
    $fields = array(
      'weight',
      'parent',
      'enabled',
    );
    $form_links = $form['links'];
    foreach (Element::children($form_links) as $id) {
      if (isset($form_links[$id]['#item'])) {
        $element = $form_links[$id];
        $updated_values = array();

        // Update any fields that have changed in this menu item.
        foreach ($fields as $field) {
          if ($element[$field]['#value'] != $element[$field]['#default_value']) {
            $updated_values[$field] = $element[$field]['#value'];
          }
        }
        if ($updated_values) {

          // Use the ID from the actual plugin instance since the hidden value
          // in the form could be tampered with.
          $this->menuLinkManager
            ->updateDefinition($element['#item']->link
            ->getPLuginId(), $updated_values);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityForm::$entityRepository protected property The entity repository service.
ContentEntityForm::$entityTypeBundleInfo protected property The entity type bundle info service.
ContentEntityForm::$time protected property The time service.
ContentEntityForm::addRevisionableFormFields protected function Add revision form fields if the entity enabled the UI.
ContentEntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityForm::buildEntity 3
ContentEntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties Overrides EntityForm::copyFormValuesToEntity
ContentEntityForm::flagViolations protected function Flags violations for the current form. 4
ContentEntityForm::form public function Gets the actual form array to be built. Overrides EntityForm::form 13
ContentEntityForm::getBundleEntity protected function Returns the bundle entity of the entity, or NULL if there is none.
ContentEntityForm::getEditedFieldNames protected function Gets the names of all fields edited in the form. 4
ContentEntityForm::getFormDisplay public function Gets the form display. Overrides ContentEntityFormInterface::getFormDisplay
ContentEntityForm::getFormLangcode public function Gets the code identifying the active form language. Overrides ContentEntityFormInterface::getFormLangcode
ContentEntityForm::getNewRevisionDefault protected function Should new revisions created on default.
ContentEntityForm::init protected function Initializes the form state and the entity before the first form build. Overrides EntityForm::init 1
ContentEntityForm::initFormLangcodes protected function Initializes form language code values.
ContentEntityForm::isDefaultFormLangcode public function Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface::isDefaultFormLangcode
ContentEntityForm::prepareEntity protected function Prepares the entity object before the form is built first. Overrides EntityForm::prepareEntity 1
ContentEntityForm::setFormDisplay public function Sets the form display. Overrides ContentEntityFormInterface::setFormDisplay
ContentEntityForm::showRevisionUi protected function Checks whether the revision form fields should be added to the form.
ContentEntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides EntityForm::submitForm 3
ContentEntityForm::updateChangedTime public function Updates the changed time of the entity.
ContentEntityForm::updateFormLangcode public function Updates the form language to reflect any change to the entity language.
ContentEntityForm::validateForm public function Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level… Overrides FormBase::validateForm 3
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actions protected function Returns an array of supported actions for the current entity form. 29
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::__get public function
EntityForm::__set public function
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OgMenuInstanceForm::$entity protected property The entity being used by this form. Overrides ContentEntityForm::$entity
OgMenuInstanceForm::$linkGenerator protected property The link generator. Overrides LinkGeneratorTrait::$linkGenerator
OgMenuInstanceForm::$menuLinkManager protected property The menu link manager.
OgMenuInstanceForm::$menuTree protected property The menu tree service.
OgMenuInstanceForm::$ogAccess protected property The OG access service.
OgMenuInstanceForm::$overviewTreeForm protected property The overview tree form.
OgMenuInstanceForm::$urlGenerator protected property The URL generator. Overrides UrlGeneratorTrait::$urlGenerator
OgMenuInstanceForm::buildForm public function Form constructor. Overrides EntityForm::buildForm
OgMenuInstanceForm::buildOverviewTreeForm protected function
OgMenuInstanceForm::create public static function Instantiates a new instance of this class. Overrides ContentEntityForm::create
OgMenuInstanceForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
OgMenuInstanceForm::submitOverviewForm protected function Submit handler for the menu overview form.
OgMenuInstanceForm::__construct public function Constructs an OgMenuInstanceForm. Overrides ContentEntityForm::__construct
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.