You are here

class MenuBadgesTabsAdminForm in Menu Badges 8

Hierarchy

Expanded class hierarchy of MenuBadgesTabsAdminForm

1 string reference to 'MenuBadgesTabsAdminForm'
menu_badges.routing.yml in ./menu_badges.routing.yml
menu_badges.routing.yml

File

src/Form/MenuBadgesTabsAdminForm.php, line 14

Namespace

Drupal\menu_badges\Form
View source
class MenuBadgesTabsAdminForm extends FormBase {

  /**
   * The menu badge manager service.
   *
   * @var \Drupal\menu_badges\MenuBadgesManager
   */
  protected $badgeManager;

  /**
   * Constructs a \Drupal\system\SystemConfigFormBase object.
   */
  public function __construct(MenuBadgesManager $badgeManager) {
    $this->badgeManager = $badgeManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('menu_badges.manager'));
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::getFormID().
   */
  public function getFormID() {
    return 'menu_badges_tabs_admin';
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $badges = $this->badgeManager
      ->getLocalBadgesForRoutes();
    kint($badges);
    $form['#tree'] = TRUE;

    //$form['#attached']['css'] = array(drupal_get_path('module', 'menu_badges') . '/menu_badges.css');
    $form['search'] = array(
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Search'),
    );
    $form['search']['title'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Title'),
      '#size' => 30,
    );
    $form['search']['path'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Menu Path'),
      '#size' => 30,
    );
    $route_types = array(
      MenuBadgesManager::LOCAL_TASK => 'Tab',
      MenuBadgesManager::LOCAL_ACTION => 'Action Link',
    );
    $form['search']['type'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Menu Type'),
      '#options' => array(
        '' => '',
      ) + $route_types,
    );
    $form['search']['search'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Search'),
    );
    $search_types = array();
    $values = $form_state
      ->getValues();
    if (!empty($values['search']['type'])) {
      $search_types = array(
        $form_state
          ->getValue([
          'search',
          'type',
        ]),
      );
    }
    $search_title = !empty($values['search']['title']) ? $values['search']['title'] : NULL;
    $search_path = !empty($values['search']['path']) ? $values['search']['path'] : NULL;
    $routes = $this->badgeManager
      ->getLocalRoutes($search_types, $search_title, $search_path);
    $manager = \Drupal::service('plugin.manager.link_badge');
    $definitions = $manager
      ->getDefinitions();
    $menu_badge_options = array(
      '' => t('None'),
    );
    foreach ($definitions as $d) {
      $menu_badge_options[$d['id']] = $d['label'];
    }
    $form['results'] = array();
    foreach ($routes as $route_id => $record) {

      // Make the route ID specific to type
      $route_type_and_id = $record['menu_badges_route_type'] . '||' . str_replace('.', '|', $route_id);
      $route_id = str_replace('.', '|', $route_id);
      $form['results'][$route_type_and_id] = array();
      $form['results'][$route_type_and_id]['path'] = array(
        '#type' => 'value',
        '#value' => $record['menu_badges_route_path'],
      );
      $form['results'][$route_type_and_id]['title'] = array(
        '#type' => 'value',
        '#value' => $record['title'],
      );
      $form['results'][$route_type_and_id]['type'] = array(
        '#type' => 'value',
        '#value' => $route_types[$record['menu_badges_route_type']],
      );
      $form['results'][$route_type_and_id]['menu_badges_id'] = array(
        '#type' => 'select',
        '#options' => $menu_badge_options,
        '#default_value' => !empty($badges[$record['menu_badges_route_type']][$route_id]) ? $badges[$record['menu_badges_route_type']][$route_id]['id'] : '',
      );
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
    );
    $form['#theme'] = 'menu_badges_tabs_admin_form';
    return $form;
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::submitForm().
   */
  public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
    $triggering_element = $form_state
      ->getTriggeringElement();
    $is_search = $triggering_element['#parents'][0] == 'search' && $triggering_element['#parents'][1] == 'search';
    if ($is_search) {
      $form_state
        ->setRebuild(TRUE);
    }
    else {
      $form_state
        ->setRebuild(TRUE);
      $local_badges = $this->badgeManager
        ->getLocalBadges();
      kint($local_badges);
      foreach ($form_state
        ->getValue('results') as $route_id => $route) {
        $route_desc = explode('||', $route_id);
        if (!empty($route['menu_badges_id'])) {
          $local_badges[$route_desc[0]][$route_desc[1]] = [
            'id' => $route['menu_badges_id'],
          ];
        }
        elseif (!empty($local_badges[$route_desc[0]][$route_desc[1]])) {
          unset($local_badges[$route_desc[0]][$route_desc[1]]);
        }
      }
      $this->badgeManager
        ->setLocalBadges($local_badges);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
FormInterface::getFormId public function Returns a unique string identifying the form. 236
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
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.
MenuBadgesTabsAdminForm::$badgeManager protected property The menu badge manager service.
MenuBadgesTabsAdminForm::buildForm public function Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides FormInterface::buildForm
MenuBadgesTabsAdminForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
MenuBadgesTabsAdminForm::getFormID public function Implements \Drupal\Core\Form\FormInterface::getFormID().
MenuBadgesTabsAdminForm::submitForm public function Implements \Drupal\Core\Form\FormInterface::submitForm(). Overrides FormInterface::submitForm
MenuBadgesTabsAdminForm::__construct public function Constructs a \Drupal\system\SystemConfigFormBase object.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
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::$urlGenerator protected property The url generator.
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.