class MenuBadgesTabsAdminForm in Menu Badges 8
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\menu_badges\Form\MenuBadgesTabsAdminForm
Expanded class hierarchy of MenuBadgesTabsAdminForm
1 string reference to 'MenuBadgesTabsAdminForm'
File
- src/
Form/ MenuBadgesTabsAdminForm.php, line 14
Namespace
Drupal\menu_badges\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
FormInterface:: |
public | function | Returns a unique string identifying the form. | 236 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MenuBadgesTabsAdminForm:: |
protected | property | The menu badge manager service. | |
MenuBadgesTabsAdminForm:: |
public | function |
Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides FormInterface:: |
|
MenuBadgesTabsAdminForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
MenuBadgesTabsAdminForm:: |
public | function | Implements \Drupal\Core\Form\FormInterface::getFormID(). | |
MenuBadgesTabsAdminForm:: |
public | function |
Implements \Drupal\Core\Form\FormInterface::submitForm(). Overrides FormInterface:: |
|
MenuBadgesTabsAdminForm:: |
public | function | Constructs a \Drupal\system\SystemConfigFormBase object. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |