foldershare-8.x-1.2/src/Form/AdminSettingsTraits/AdminSettingsInterfaceTab.php

src/Form/AdminSettingsTraits/AdminSettingsInterfaceTab.php
<?php
 
namespace Drupal\foldershare\Form\AdminSettingsTraits;
 
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
 
use Drupal\foldershare\Constants;
use Drupal\foldershare\Settings;
use Drupal\foldershare\Entity\FolderShare;
use Drupal\foldershare\Utilities\ConfigurationUtilities;
use Drupal\foldershare\Utilities\LinkUtilities;
 
/**
 * Manages the "Interface" tab for the module's settings form.
 *
 * <B>Warning:</B> This is an internal trait that is strictly used by
 * the AdminSettings form class. It is a mechanism to group functionality
 * to improve code management.
 *
 * @ingroup foldershare
 */
trait AdminSettingsInterfaceTab {
 
  /*---------------------------------------------------------------------
   *
   * Build.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Builds the user interface tab.
   *
   * @param array $form
   *   An associative array containing the structure of the form. The form
   *   is modified to include additional render elements for the tab.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The current state of the form.
   * @param string $tabGroup
   *   The name of the tab group.
   */
  private function buildInterfaceTab(
    array &$form,
    FormStateInterface $formState,
    string $tabGroup) {
 
    //
    // Find installed modules.
    // -----------------------
    // Tab sections vary their presentation based upon what modules are
    // installed at the site.
    $mh = \Drupal::service('module_handler');
 
    $helpInstalled     = $mh->moduleExists('help');
    $viewsUiInstalled  = $mh->moduleExists('views_ui');
    $realnameInstalled = $mh->moduleExists('realname');
    $fieldUiInstalled  = $mh->moduleExists('field_ui');
 
    //
    // Set up tab names.
    // -----------------
    // Create the machine name for the tab and the tab's title.
    $tabMachineName = 'interface';
    $tabTitle       = $this->t('Interface');
    $tabPaneTitle   = $this->t('Manage the user interface');
 
    //
    // Set up class names.
    // -------------------
    // Use a set of standard class names for tab sections.
    $moduleName = self::makeCssSafe(Constants::MODULE);
 
    $tabName = $moduleName . '_' . $tabMachineName . '_tab';
 
    $tabSubtitleClass        = $moduleName . '-settings-subtitle';
    $sectionWrapperClass     = $moduleName . '-settings-section-wrapper';
    $sectionClass            = $moduleName . '-settings-section';
    $sectionDescriptionClass = $moduleName . '-settings-section-description';
    $itemDefaultClass        = $moduleName . '-settings-item-default';
    $itemNoteClass           = $moduleName . '-settings-item-note';
    $warningClass            = $moduleName . '-warning';
    $noteClass               = $moduleName . '-note';
 
    $menuAllowAllCheck       = $moduleName . '_menu_allow_all';
    $menuAllowedCommandsList = $moduleName . '_menu_allowed_commands';
    $restoreMenuButton       = $moduleName . '_menu_restore';
    $restoreViewButton       = $moduleName . '_view_restore';
    $restoreFormsButton      = $moduleName . '_forms_restore';
    $restoreDisplaysButton   = $moduleName . '_displays_restore';
    $userautocompleteClass   = $moduleName . '-settings-userautocomplete';
    $userautocomplete        = $moduleName . '_userautocomplete';
 
    //
    // Create the tab
    // --------------
    // Start the tab with a title, subtitle, and description.
    $form[$tabName] = [
      '#type'                    => 'details',
      '#open'                    => FALSE,
      '#group'                   => $tabGroup,
      '#title'                   => $tabTitle,
 
      '#description'             => [
        'subtitle'               => [
          '#type'                => 'html_tag',
          '#tag'                 => 'h2',
          '#value'               => $tabPaneTitle,
          '#attributes'          => [
            'class'              => [$tabSubtitleClass],
          ],
        ],
 
        'description'            => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t(
            'Control forms, pages, menus, file and folder lists, and autocomplete prompts.'),
          '#attributes'          => [
            'class'              => [
              $moduleName . '-settings-description',
            ],
          ],
        ],
      ],
      '#attributes'              => [
        'class'                  => [
          $moduleName . '-settings-tab ',
          $moduleName . '-interface-tab',
        ],
      ],
    ];
 
    //
    // Manage fields, forms, and displays.
    // -----------------------------------
    //
    // FIELD UI  HELP  MESSAGE
    // no        no    basic message
    // no        yes   basic message + field help
    // yes       no    basic message + field UI links
    // yes       yes   basic message + field UI links + field and field UI help
    //
    // The Field module is always installed since it is a required part of core.
    if ($fieldUiInstalled === TRUE) {
      $fieldUiName = $mh->getName('field_ui');
    }
    else {
      $fieldUiName = 'Field UI';
    }
 
    if ($fieldUiInstalled === TRUE &&
        $helpInstalled === TRUE) {
      // Field UI is installed and help is available.
      $description = $this->t(
        'Files and folders may be given additional @fieldUiFieldLink, and their @fieldUiFormLink and @fieldUiDisplayLink configured using the %fieldUiName module (see @fieldHelpLink and @fieldUiHelpLink).',
        [
          '%fieldUiName'         => $fieldUiName,
          '@fieldUiFieldLink'    => LinkUtilities::createRouteLink(
            'entity.' . FolderShare::ENTITY_TYPE_ID . '.field_ui_fields',
            '',
            $this->t('fields')),
          '@fieldUiFormLink'     => LinkUtilities::createRouteLink(
            'entity.entity_form_display.' . FolderShare::ENTITY_TYPE_ID . '.default',
            '',
            $this->t('forms')),
          '@fieldUiDisplayLink'  => LinkUtilities::createRouteLink(
            'entity.entity_view_display.' . FolderShare::ENTITY_TYPE_ID . '.default',
            '',
            $this->t('page displays')),
          '@fieldHelpLink'       => LinkUtilities::createHelpLink(
            'field',
            $this->t('Field help')),
          '@fieldUiHelpLink'     => LinkUtilities::createHelpLink(
            'field_ui',
            $this->t('Field UI help')),
        ]);
      $restoreFormDescription = $this->t(
        'Restore @fieldUiFormLink to default values.',
        [
          '@fieldUiFormLink'     => LinkUtilities::createRouteLink(
            'entity.entity_form_display.' . FolderShare::ENTITY_TYPE_ID . '.default',
            '',
            $this->t('forms')),
        ]);
      $restoreDisplaysDescription = $this->t(
        'Restore @fieldUiDisplayLink to default values.',
        [
          '@fieldUiDisplayLink'  => LinkUtilities::createRouteLink(
            'entity.entity_view_display.' . FolderShare::ENTITY_TYPE_ID . '.default',
            '',
            $this->t('page displays')),
        ]);
    }
    elseif ($fieldUiInstalled === TRUE &&
            $helpInstalled === FALSE) {
      // Field UI is installed but there is no help.
      $description = $this->t(
        'Files and folders may be given additional @fieldUiFieldLink, and their @fieldUiFormLink and @fieldUiDisplayLink configured using the %fieldUiName module.',
        [
          '%fieldUiName'         => $fieldUiName,
          '@fieldUiFieldLink'    => LinkUtilities::createRouteLink(
            'entity.' . FolderShare::ENTITY_TYPE_ID . '.field_ui_fields',
            '',
            $this->t('fields')),
          '@fieldUiFormLink'     => LinkUtilities::createRouteLink(
            'entity.entity_form_display.' . FolderShare::ENTITY_TYPE_ID . '.default',
            '',
            $this->t('forms')),
          '@fieldUiDisplayLink'  => LinkUtilities::createRouteLink(
            'entity.entity_view_display.' . FolderShare::ENTITY_TYPE_ID . '.default',
            '',
            $this->t('page displays')),
        ]);
      $restoreFormDescription = $this->t(
        'Restore @fieldUiFormLink to default values.',
        [
          '@fieldUiFormLink'     => LinkUtilities::createRouteLink(
            'entity.entity_form_display.' . FolderShare::ENTITY_TYPE_ID . '.default',
            '',
            $this->t('forms')),
        ]);
      $restoreDisplaysDescription = $this->t(
        'Restore @fieldUiDisplayLink to default values.',
        [
          '@fieldUiDisplayLink'  => LinkUtilities::createRouteLink(
            'entity.entity_view_display.' . FolderShare::ENTITY_TYPE_ID . '.default',
            '',
            $this->t('page displays')),
        ]);
    }
    else {
      // All other cases.
      $description = $this->t(
        'Files and folders may be given additional fields, and their forms and page displays configured by installing the %fieldUiEnableLink module.',
        [
          '%fieldUiEnableLink'   => LinkUtilities::createRouteLink(
            'system.modules_list',
            'module-field-ui',
            $fieldUiName),
        ]);
      $restoreFormDescription = $this->t(
        'Restore forms to default values.');
      $restoreDisplaysDescription = $this->t(
        'Restore page displays to default values.');
    }
 
    $form[$tabName]['manage-forms'] = [
      '#type'                    => 'details',
      '#title'                   => $this->t('Forms and pages'),
      '#open'                    => FALSE,
      '#attributes'              => [
        'class'                  => [$sectionWrapperClass],
      ],
 
      'section'                  => [
        '#type'                  => 'container',
        '#attributes'            => [
          'class'                => [$sectionClass],
        ],
 
        'section-description'    => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $description,
          '#attributes'          => [
            'class'              => [$sectionDescriptionClass],
          ],
        ],
 
        'forms-pair'             => [
          '#type'                => 'container',
          // A button to restore the default form configuration.
          $restoreFormsButton    => [
            '#type'              => 'submit',
            '#value'             => $this->t('Restore'),
            '#name'              => $restoreFormsButton,
            '#attributes'        => [
              'class'            => [
                $restoreFormsButton,
              ],
            ],
          ],
 
          // A description for the button.
          'forms-restore-description' => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $restoreFormDescription,
            '#attributes'        => [
              'class'            => [
                $restoreFormsButton . '_description',
              ],
            ],
          ],
        ],
 
        'displays-pair'          => [
          '#type'                => 'container',
          // A button to restore the default page display configuration.
          $restoreDisplaysButton => [
            '#type'              => 'submit',
            '#value'             => $this->t('Restore'),
            '#name'              => $restoreDisplaysButton,
            '#attributes'        => [
              'class'            => [
                $restoreDisplaysButton,
              ],
            ],
          ],
 
          // A description for the button.
          'displays-restore-description' => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $restoreDisplaysDescription,
            '#attributes'        => [
              'class'            => [
                $restoreDisplaysButton . '_description',
              ],
            ],
          ],
        ],
      ],
    ];
 
    //
    // Manage file & folder lists.
    // ---------------------------
    // Add a restore button to reset the view configuration.
    //
    // The description changes a bit for different configurations:
    //
    // VIEWS  VIEWS UI  HELP  MESSAGE
    // yes    no        no    basic message + views ui enable
    // yes    no        yes   basic message + views ui enable + help
    // yes    yes       no    basic message + views form
    // yes    yes       yes   basic message + views form + help
    //
    // The Views module is always enabled in these choices because FolderShare
    // requires it.
    if ($viewsUiInstalled === TRUE) {
      $viewsUiName = $mh->getName('views_ui');
    }
    else {
      $viewsUiName = 'Views UI';
    }
 
    $viewsName = $mh->getName('views');
 
    $viewsUiEnableLink = LinkUtilities::createRouteLink(
      'system.modules_list',
      'module-viewsui',
      $viewsUiName);
 
    if ($viewsUiInstalled === TRUE &&
        $helpInstalled === TRUE) {
      // Views UI installed and help available.
      $description = $this->t(
        'A @viewLink for the %viewsName module (see @viewsHelpLink) creates lists of files and folders. Use the %viewsUiLink module (see @viewsUiHelpLink) to configure lists.',
        [
          '%viewsName'       => $viewsName,
          '@viewLink'        => LinkUtilities::createRouteLink(
            'entity.view.edit_form',
            '',
            $this->t('view'),
            [
              'view'         => Constants::VIEW_LISTS,
            ]),
          '%viewsUiLink'     => LinkUtilities::createRouteLink(
            'entity.view.collection',
            '',
            $viewsUiName),
          '@viewsHelpLink'   => LinkUtilities::createHelpLink(
            'views',
            $this->t('help')),
          '@viewsUiHelpLink' => LinkUtilities::createHelpLink(
            'views_ui',
            $this->t('help')),
        ]);
 
      $buttonDescription = $this->t(
        'Restore @viewLink to default values.',
        [
          '@viewLink' => LinkUtilities::createRouteLink(
            'entity.view.edit_form',
            '',
            $this->t('view'),
            [
              'view' => Constants::VIEW_LISTS,
            ]),
        ]);
    }
    elseif ($viewsUiInstalled === TRUE &&
            $helpInstalled === FALSE) {
      // Views UI installed but no help.
      $description = $this->t(
        'A @viewLink for the %viewsName module creates lists of files and folders. Use the %viewsUiLink module to configure lists.',
        [
          '%viewsName'       => $viewsName,
          '@viewLink' => LinkUtilities::createRouteLink(
            'entity.view.edit_form',
            '',
            $this->t('view'),
            [
              'view' => Constants::VIEW_LISTS,
            ]),
          '%viewsUiLink' => LinkUtilities::createRouteLink(
            'entity.view.collection',
            '',
            $viewsUiName),
        ]);
 
      $buttonDescription = $this->t(
        'Restore @viewLink to default values.',
        [
          '@viewLink' => LinkUtilities::createRouteLink(
            'entity.view.edit_form',
            '',
            $this->t('view'),
            [
              'view' => Constants::VIEW_LISTS,
            ]),
        ]);
    }
    elseif ($viewsUiInstalled === FALSE &&
            $helpInstalled === TRUE) {
      // Views UI is not installed, but there is help.
      $description = $this->t(
        'A view for the %viewsName module (see @viewsHelpLink) creates lists of files and folders. Install the %viewsUiEnableLink module to configure lists.',
        [
          '%viewsName'           => $viewsName,
          '%viewsUiEnableLink'   => $viewsUiEnableLink,
          '@viewsHelpLink'       => LinkUtilities::createHelpLink(
            'views',
            $this->t('help')),
        ]);
 
      $buttonDescription = $this->t('Restore view to default values.');
    }
    else {
      // All other cases.
      $description = $this->t(
        'A view for the %viewsName module creates lists of files and folders. Install the %viewsUiEnableLink module to configure lists.',
        [
          '%viewsName'           => $viewsName,
          '%viewsUiEnableLink'   => $viewsUiEnableLink,
        ]);
 
      $buttonDescription = $this->t('Restore view to default values.');
    }
 
    $form[$tabName]['manage-views-ui'] = [
      '#type'                    => 'details',
      '#title'                   => $this->t('File & folder lists'),
      '#attributes'              => [
        'class'                  => [$sectionWrapperClass],
      ],
 
      'section'                  => [
        '#type'                  => 'container',
        '#attributes'            => [
          'class'                => [$sectionClass],
        ],
        'section-description'    => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $description,
          '#attributes'          => [
            'class'              => [$sectionDescriptionClass],
          ],
        ],
 
        // A button to restore the default configuration.
        $restoreViewButton       => [
          '#type'                => 'submit',
          '#value'               => $this->t('Restore'),
          '#name'                => $restoreViewButton,
          '#attributes'          => [
            'class'              => [
              $restoreViewButton,
            ],
          ],
        ],
 
        // A description for the button.
        'view-restore-description' => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $buttonDescription,
          '#attributes'          => [
            'class'              => [
              $restoreViewButton . '_description',
            ],
          ],
        ],
 
        // A warning of ViewsUI is not installed.
        'viewsui-disabled-warning' => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t(
            'View editing features are disabled because the @viewsUiEnableLink module is not installed.',
            [
              '@viewsUiEnableLink' => $viewsUiEnableLink,
            ]),
          '#attributes'          => [
            'class'              => [$warningClass],
          ],
        ],
      ],
    ];
 
    // If the view_ui module is not installed, links to the views config
    // pages will not work. Create a warning.
    if ($viewsUiInstalled === TRUE) {
      // Views UI is installed. Remove the warning about it not being installed.
      unset($form[$tabName]['manage-views-ui']['section']['viewsui-disabled-warning']);
    }
 
    //
    // Manage menus.
    // -------------
    // Add a checkbox to enable/disable menu restrictions, and a list of
    // checkboxes to enable/disable specific menu items.
    //
    // Get the unsorted list of all menu command definitions. Array keys are
    // IDs and values are definitions.
    $defs = Settings::getAllCommandDefinitions();
 
    // Sort them, reordering the keys to sort by definition label.
    usort(
      $defs,
      function ($a, $b) {
        if ($a['label'] == $b['label']) {
          return ($a['id'] < $b['id']) ? (-1) : 1;
        }
 
        return ($a['label'] < $b['label']) ? (-1) : 1;
      });
 
    // Build a sorted list of menu commands, with descriptions, for use in
    // creating checkboxes.
    $allNames = [];
    $allChosen = [];
 
    foreach ($defs as $def) {
      $id          = $def['id'];
      $label       = $def['label'];
      $provider    = $def['provider'];
      $description = $def['description'];
 
      $allNames[$id] = "<strong>$label</strong> (<em>$id</em>)<p>$description <em>($provider module)</em></p>";
      $allChosen[$id] = NULL;
    }
 
    foreach (Settings::getCommandMenuAllowed() as $id) {
      $allChosen[$id] = $id;
    }
 
    $form[$tabName]['manage-menus'] = [
      '#type'                    => 'details',
      '#title'                   => $this->t('Menus'),
      '#open'                    => FALSE,
      '#attributes'              => [
        'class'                  => [$sectionWrapperClass],
      ],
 
      'section'                  => [
        '#type'                  => 'container',
        '#attributes'            => [
          'class'                => [$sectionClass],
        ],
 
        'section-description'    => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t(
            'Menu item plugins support upload, download, copy, move, delete, and so forth.'),
          '#attributes'          => [
            'class'              => [$sectionDescriptionClass],
          ],
        ],
 
        // Checkbox to enable/disable restrictions.
        $menuAllowAllCheck       => [
          '#type'                => 'checkbox',
          '#title'               => $this->t('Allow all menu items'),
          '#default_value'       => (Settings::getCommandMenuRestrict() === FALSE),
          '#return_value'        => 'enabled',
          '#required'            => FALSE,
          '#name'                => $menuAllowAllCheck,
        ],
 
        // The default setting.
        'menu-restrictions-default' => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t('Default: Allow all menu items'),
          '#attributes'          => [
            'class'              => [
              $itemDefaultClass,
              $itemNoteClass,
            ],
          ],
        ],
 
        // List of menu commands.
        'foldershare_command_menu_choices' => [
          '#type'                => 'container',
          '#attributes'          => [
            'class'              => ['foldershare_command_menu_choices'],
          ],
          '#states'              => [
            'invisible'          => [
              'input[name="' . $menuAllowAllCheck . '"]' => [
                'checked'        => TRUE,
              ],
            ],
          ],
 
          // A prompt before the list of checkboxes.
          'description'          => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $this->t('Select the menu items to allow:'),
          ],
 
          // The list of checkboxes.
          $menuAllowedCommandsList => [
            '#type'              => 'checkboxes',
            '#options'           => $allNames,
            '#default_value'     => $allChosen,
            '#prefix'            => '<div class="' . $menuAllowedCommandsList . '">',
            '#suffix'            => '</div>',
          ],
        ],
 
        // A button to restore the default configuration.
        $restoreMenuButton => [
          '#type'                => 'submit',
          '#value'               => $this->t('Restore'),
          '#name'                => $restoreMenuButton,
          '#attributes'          => [
            'class'              => [
              $restoreMenuButton,
            ],
          ],
        ],
 
        // A description for the button.
        'menu-restore-description' => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t('Restore menus to default values.'),
          '#attributes'          => [
            'class'              => [
              $restoreMenuButton . '_description',
            ],
          ],
        ],
      ],
    ];
 
    //
    // Manage user autocomplete.
    // -------------------------
    // Add information and a menu to select the user autocomplete style.
    //
    // The select menu options remain the same, but the text changes a bit
    // depending upon what modules are installed. The descriptions also
    // vary a bit.
    //
    // REALNAME  OPTIONS
    // no        Account names...
    // yes       User names...
    //
    // REALNAME  HELP  OPTIONS
    // no        no    basic message.
    // no        yes   basic message.
    // yes       no    basic message.
    // yes       yes   basic message + help.
    if ($realnameInstalled === TRUE) {
      // Realname module installed.
      $realnameName = $mh->getName('realname');
      $userautocompleteOptions = [
        'none'              => $this->t('Disable user autocomplete'),
        'name-only'         => $this->t('Show user names'),
        'name-email'        => $this->t('Show user names and email addresses'),
        'name-masked-email' => $this->t('Show user names and partially masked email addresses'),
      ];
 
      $description = $this->t(
        'Autocomplete menus help to select among users.');
      $defaultDescription = t('Default: Show user names');
 
      $realnameLink = LinkUtilities::createRouteLink(
        'realname.admin_settings_form',
        '',
        $realnameName);
 
      if ($helpInstalled === TRUE) {
        // Help is installed.
        $realnameHelpLink = LinkUtilities::createHelpLink(
          'realname',
          $this->t('help'));
 
        $nameOnlyDescription = t(
          'Autocomplete menus show full user names configured by the %realnameLink module (see @realnameHelpLink).',
          [
            '%realnameLink'     => $realnameLink,
            '@realnameHelpLink' => $realnameHelpLink,
          ]);
        $nameEmailDescription = t(
          'Autocomplete menus full user names configured by the %realnameLink module (see @realnameHelpLink). Full email addresses are included',
          [
            '%realnameLink'     => $realnameLink,
            '@realnameHelpLink' => $realnameHelpLink,
          ]);
        $nameMaskedEmailDescription = t(
          'Autocomplete menus show full user names configured by the %realnameLink module (see @realnameHelpLink). Partially masked email addresses are included (e.g. "a****b@example.com").',
          [
            '%realnameLink'     => $realnameLink,
            '@realnameHelpLink' => $realnameHelpLink,
          ]);
      }
      else {
        $nameOnlyDescription = t(
          'Autocomplete menus show full user names configured by the %realnameLink module.',
          [
            '%realnameLink'     => $realnameLink,
          ]);
        $nameEmailDescription = t(
          'Autocomplete menus full user names configured by the %realnameLink module. Full email addresses are included',
          [
            '%realnameLink'     => $realnameLink,
          ]);
        $nameMaskedEmailDescription = t(
          'Autocomplete menus show full user names configured by the %realnameLink module. Partially masked email addresses are included (e.g. "a****b@example.com").',
          [
            '%realnameLink'     => $realnameLink,
          ]);
      }
    }
    else {
      // Realname module is not installed.
      $realnameName = 'Real name';
      $userautocompleteOptions = [
        'none'              => $this->t('Disable user autocomplete'),
        'name-only'         => $this->t('Show account names'),
        'name-email'        => $this->t('Show account names and email addresses'),
        'name-masked-email' => $this->t('Show account names and partially masked email addresses'),
      ];
      $description = $this->t(
        'Autocomplete menus help to select among users. Install the %realnameProjectLink contributed module to show full user names.',
        [
          '%realnameProjectLink' => LinkUtilities::createProjectLink(
            'realname',
            $realnameName),
        ]);
      $defaultDescription = t('Default: Show account names');
 
      $nameOnlyDescription = t('Autocomplete menus show user account names.');
      $nameEmailDescription = t('Autocomplete menus show user account names along with full email addresses.');
      $nameMaskedEmailDescription = t('Autocomplete menus show user account names along with partially masked email addresses (e.g. "a****b@example.com").');
    }
 
    $form[$tabName]['manage-user-autocomplete'] = [
      '#type'                    => 'details',
      '#title'                   => $this->t('User autocomplete'),
      '#attributes'              => [
        'class'                  => [$sectionWrapperClass],
      ],
 
      'section'                  => [
        '#type'                  => 'container',
        '#attributes'            => [
          'class'                => [$sectionClass],
        ],
 
        'section-description'    => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $description,
          '#attributes'          => [
            'class'              => [$sectionDescriptionClass],
          ],
        ],
 
        // Autocomplete menu.
        $userautocomplete        => [
          '#type'                => 'select',
          '#options'             => $userautocompleteOptions,
          '#default_value'       => Settings::getUserAutocompleteStyle(),
          '#name'                => $userautocomplete,
          '#attributes'          => [
            'class'              => [
              $userautocompleteClass,
            ],
          ],
        ],
 
        // The default setting.
        'user-autocomplete-default' => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $defaultDescription,
          '#attributes'          => [
            'class'              => [
              $itemDefaultClass,
              $itemNoteClass,
            ],
          ],
        ],
 
        // A note when names only.
        'userautocompleteNameOnly' => [
          '#type'                => 'container',
          '#attributes'          => [
            'class'              => [
              $itemNoteClass,
            ],
          ],
          '#states'              => [
            'visible'            => [
              'select[name="' . $userautocomplete . '"]' => [
                'value'          => 'name-only',
              ],
            ],
          ],
 
          'note'                 => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $nameOnlyDescription,
          ],
        ],
 
        // A note when users with email.
        'userautocompleteEmail'  => [
          '#type'                => 'container',
          '#attributes'          => [
            'class'              => [
              $itemNoteClass,
            ],
          ],
          '#states'              => [
            'visible'            => [
              'select[name="' . $userautocomplete . '"]' => [
                'value'          => 'name-email',
              ],
            ],
          ],
 
          'note'                 => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $nameEmailDescription,
          ],
 
          'warning'              => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $this->t(
              'Email addresses are often considered private information. Use this option with caution.'),
            '#attributes'        => [
              'class'            => [
                $noteClass,
              ],
            ],
          ],
        ],
 
        // A note when users with masked email.
        'userautocompleteMaskedEmail' => [
          '#type'                => 'container',
          '#attributes'          => [
            'class'              => [
              $itemNoteClass,
            ],
          ],
          '#states'              => [
            'visible'            => [
              'select[name="' . $userautocomplete . '"]' => [
                'value'          => 'name-masked-email',
              ],
            ],
          ],
 
          'note'                 => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $nameMaskedEmailDescription,
          ],
 
          'warning'              => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $this->t(
             'Email addresses are often considered private information. Partially masking an email address may not be sufficient to protect it. Use this option with caution.'),
            '#attributes'        => [
              'class'            => [
                $noteClass,
              ],
            ],
          ],
        ],
      ],
    ];
  }
 
  /*---------------------------------------------------------------------
   *
   * Validate.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Validates form values.
   *
   * @param array $form
   *   The form configuration.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The entered values for the form.
   */
  private function validateInterfaceTab(
    array &$form,
    FormStateInterface &$formState) {
 
    $moduleName = Constants::MODULE;
    $trigger    = $formState->getTriggeringElement();
    $userInput  = $formState->getUserInput();
 
    $menuAllowAllCheck       = $moduleName . '_menu_allow_all';
    $menuAllowedCommandsList = $moduleName . '_menu_allowed_commands';
    $restoreMenuButton       = $moduleName . '_menu_restore';
    $restoreViewButton       = $moduleName . '_view_restore';
    $restoreFormsButton      = $moduleName . '_forms_restore';
    $restoreDisplaysButton   = $moduleName . '_displays_restore';
 
    //
    // Restore menu commands.
    // ----------------------
    // Restore command restrictions to an original configuration.
    if ($trigger['#name'] === $restoreMenuButton) {
      // Reset menu command configuration.
      Settings::setCommandMenuRestrict(
        Settings::getCommandMenuRestrictDefault());
      Settings::setCommandMenuAllowed(
        Settings::getCommandMenuAllowedDefault());
 
      $formState->setRebuild(TRUE);
      $formState->cleanValues();
      $formState->setTriggeringElement(NULL);
 
      // Update form state.
      foreach (Settings::getCommandMenuAllowed() as $id) {
        $allChosen[$id] = $id;
      }
 
      $userInput[$menuAllowedCommandsList] = $allChosen;
      $userInput[$menuAllowAllCheck] = (Settings::getCommandMenuRestrict() === FALSE);
      unset($userInput[$restoreMenuButton]);
      $formState->setUserInput($userInput);
 
      \Drupal::messenger()->addMessage(
        $this->t('Menus have been restored to their defaults.'),
        'status');
      return;
    }
 
    //
    // Restore view.
    // -------------
    // Restore the view to an original configuration.
    if ($trigger['#name'] === $restoreViewButton) {
      // Restore the view.
      $status = ConfigurationUtilities::revertConfiguration(
        'view',
        Constants::VIEW_LISTS);
 
      $formState->setRebuild(TRUE);
      $formState->cleanValues();
      $formState->setTriggeringElement(NULL);
 
      if ($status === TRUE) {
        \Drupal::messenger()->addMessage(
          $this->t('File and folder views have been restored to their defaults.'),
          'status');
      }
      else {
        \Drupal::messenger()->addMessage(
          $this->t('File and folder views could not be restored to their defaults due to an unexpected problem.'),
          'error');
      }
 
      return;
    }
 
    //
    // Restore forms.
    // --------------
    // Restore the configuration.
    if ($trigger['#name'] === $restoreFormsButton) {
      // Revert form configuration.
      $status = ConfigurationUtilities::revertConfiguration(
        'core',
        'entity_form_display.foldershare.foldershare.default');
 
      $formState->setRebuild(TRUE);
      $formState->cleanValues();
      $formState->setTriggeringElement(NULL);
 
      if ($status === TRUE) {
        \Drupal::messenger()->addMessage(
          $this->t('File and folder forms have been restored to their defaults.'),
          'status');
      }
      else {
        \Drupal::messenger()->addMessage(
          $this->t('File and folder forms could not be restored to their defaults due to an unexpected problem.'),
          'error');
      }
 
      return;
    }
 
    //
    // Restore displays.
    // -----------------
    // Restore the configuraiton.
    if ($trigger['#name'] === $restoreDisplaysButton) {
      // Revert form configuration.
      $status = ConfigurationUtilities::revertConfiguration(
        'core',
        'entity_view_display.foldershare.foldershare.default');
 
      $formState->setRebuild(TRUE);
      $formState->cleanValues();
      $formState->setTriggeringElement(NULL);
 
      if ($status === TRUE) {
        \Drupal::messenger()->addMessage(
          $this->t('File and folder page displays have been restored to their defaults.'),
          'status');
      }
      else {
        \Drupal::messenger()->addMessage(
          $this->t('File and folder page displays could not be restored to their defaults due to an unexpected problem.'),
          'error');
      }
 
      return;
    }
  }
 
  /*---------------------------------------------------------------------
   *
   * Submit.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Stores submitted form values.
   *
   * @param array $form
   *   The form configuration.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The entered values for the form.
   */
  private function submitInterfaceTab(
    array &$form,
    FormStateInterface &$formState) {
 
    $moduleName              = Constants::MODULE;
    $menuAllowAllCheck       = $moduleName . '_menu_allow_all';
    $menuAllowedCommandsList = $moduleName . '_menu_allowed_commands';
    $userautocomplete        = $moduleName . '_userautocomplete';
 
    // Get whether the command menu is restricted.
    $value = $formState->getValue($menuAllowAllCheck);
    $enabled = ($value === 'enabled') ? TRUE : FALSE;
    Settings::setCommandMenuRestrict($enabled === FALSE);
 
    // Get allowed menu commands.
    $allChosen = $formState->getValue($menuAllowedCommandsList);
    Settings::setCommandMenuAllowed(array_keys(array_filter($allChosen)));
 
    // Get user autocomplete style.
    $userautocompleteStyle = $formState->getValue($userautocomplete);
    Settings::setUserAutocompleteStyle($userautocompleteStyle);
 
    Cache::invalidateTags(['rendered']);
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc