class CustomBreadcrumbsForm in Custom Breadcrumbs 1.x
Custom breadcrumbs form.
@property \Drupal\custom_breadcrumbs\CustomBreadcrumbsInterface $entity
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\custom_breadcrumbs\Form\CustomBreadcrumbsForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of CustomBreadcrumbsForm
1 file declares its use of CustomBreadcrumbsForm
File
- src/
Form/ CustomBreadcrumbsForm.php, line 18
Namespace
Drupal\custom_breadcrumbs\FormView source
class CustomBreadcrumbsForm extends EntityForm {
/**
* EntityTypeBundleInfo.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Language interface.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* CustomBreadcrumbsForm constructor.
*
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
* EntityTypeBundleInfo service.
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* LanguageManager service.
*/
public function __construct(EntityTypeBundleInfoInterface $entityTypeBundleInfo, LanguageManagerInterface $languageManager) {
$this->entityTypeBundleInfo = $entityTypeBundleInfo;
$this->languageManager = $languageManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.bundle.info'), $container
->get('language_manager'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $this->entity
->label(),
'#description' => $this
->t('Label for the custom breadcrumb.'),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $this->entity
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\custom_breadcrumbs\\Entity\\CustomBreadcrumbs::load',
],
'#disabled' => !$this->entity
->isNew(),
];
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enabled'),
'#default_value' => $this->entity
->status(),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#default_value' => $this->entity
->get('description'),
'#description' => $this
->t('Description of the custom breadcrumb.'),
];
$form['type'] = [
'#type' => 'select',
'#title' => $this
->t('Type of breadcrumb'),
'#options' => [
1 => $this
->t('Content entity'),
2 => $this
->t('Path'),
],
'#requires' => TRUE,
'#default_value' => $this->entity
->get('type'),
];
$entity = $this->entity
->get('entityType');
$entity = $form_state
->hasValue('entityType') ? $form_state
->getValue('entityType') : $entity;
$form['entityType'] = [
'#type' => 'select',
'#title' => $this
->t('Entity type'),
'#default_value' => $entity,
'#description' => $this
->t('Select your entity type.'),
'#options' => $this
->getEntityTypes(),
'#empty_value' => '_none',
'#ajax' => [
'callback' => [
$this,
'ajaxCallback',
],
'wrapper' => 'entity_bundle_configuration',
'method' => 'replace',
'effect' => 'fade',
],
'#states' => [
'visible' => [
':input[name="type"]' => [
'value' => '1',
],
],
],
];
$form['entityBundle'] = [
'#prefix' => '<div id="entity_bundle_configuration">',
'#suffix' => '</div>',
'#type' => 'select',
'#title' => $this
->t('Entity bundle'),
'#default_value' => $this->entity
->get('entityBundle'),
'#description' => $this
->t('Select your entity bundle.'),
'#options' => $this
->getEntityBundle($entity),
'#empty_value' => '_none',
'#states' => [
'visible' => [
':input[name="type"]' => [
'value' => '1',
],
],
],
];
$form['language'] = [
'#type' => 'select',
'#title' => $this
->t('Language'),
'#default_value' => $this->entity
->get('language'),
'#description' => $this
->t('Select language'),
'#options' => $this
->getAvailableLanguages(),
];
$form['pathPattern'] = [
'#type' => 'textarea',
'#title' => $this
->t('Path pattern'),
'#default_value' => $this->entity
->get('pathPattern'),
'#description' => $this
->t('A set of patterns separated by a newline. @front_key@ is used to front page. The \'*\' character is a wildcard. An example path is /admin/* for every admin pages.', [
'@front_key@' => '<front>',
]),
'#states' => [
'visible' => [
':input[name="type"]' => [
'value' => '2',
],
],
],
];
$form['breadcrumbPaths'] = [
'#type' => 'textarea',
'#title' => $this
->t('Breadcrumb paths'),
'#default_value' => $this->entity
->get('breadcrumbPaths'),
'#required' => TRUE,
'#description' => $this
->t('One url per line, you can use <a href="@token">Token</a> module. Url must start from "/". Use @nolink_key if you don\'t want to set a link for the respective title.', [
'@token' => 'https://www.drupal.org/project/token',
'@nolink_key' => '<nolink>',
]),
];
$form['breadcrumbTitles'] = [
'#type' => 'textarea',
'#title' => $this
->t('Breadcrumb titles'),
'#default_value' => $this->entity
->get('breadcrumbTitles'),
'#required' => TRUE,
'#description' => $this
->t('One title per line, you can use <a href="@token">Token</a> module.', [
'@token' => 'https://www.drupal.org/project/token',
]),
];
$form['extraCacheContexts'] = [
'#type' => 'textarea',
'#title' => $this
->t('Extra cache contexts'),
'#default_value' => $this->entity
->get('extraCacheContexts'),
'#description' => $this
->t('You can define an extra cache contexts for example for curent request query "url.query_args:search".'),
];
$form['token_tree'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
self::getTokenEntity($entity),
],
'#show_restricted' => TRUE,
'#weight' => 90,
];
$form['token_details'] = [
'#type' => 'details',
'#title' => $this
->t('Custom breadcrumbs extra vars'),
'#open' => FALSE,
];
$form['token_details']['vars'] = [
'#theme' => 'item_list',
'#items' => [
$this
->t('<nolink> - adds ability to create crumb without url'),
$this
->t('<term_hierarchy:field_name> - taxonomy term field with hierarchy'),
],
];
return $form;
}
/**
* Get token type by entity.
*
* @param string $entity_type
* Entity type.
*
* @return string
* Array of token types.
*/
public static function getTokenEntity($entity_type) {
if ($entity_type === 'taxonomy_term') {
return 'term';
}
return $entity_type;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$pages = $values['breadcrumbPaths'];
$urlList = [];
$urlList = explode(PHP_EOL, $pages);
foreach ($urlList as $url) {
$trimUrl = trim($url);
// Validate Slash.
if ($trimUrl !== '<front>' && $trimUrl !== '<nolink>' && $trimUrl[0] !== '/' && $trimUrl[0] !== '[') {
$form_state
->setErrorByName('pages', $this
->t("@url needs to start with a slash if it is a URL or with a square bracket if it is a token.", [
'@url' => $trimUrl,
]));
}
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$result = parent::save($form, $form_state);
$message_args = [
'%label' => $this->entity
->label(),
];
$message = $result == SAVED_NEW ? $this
->t('Created new custom breadcrumb %label.', $message_args) : $this
->t('Updated custom breadcrumb %label.', $message_args);
$this
->messenger()
->addStatus($message);
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
return $result;
}
/**
* Ajax callback for the entity bundle dependent configuration options.
*
* @param array $form
* Form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form State.
*
* @return array
* The form element containing the entity bundle options.
*/
public static function ajaxCallback(array &$form, FormStateInterface $form_state) {
return $form['entityBundle'];
}
/**
* Get all available content entity types.
*
* @return array
* Array of entity types.
*/
private function getEntityTypes() {
$options = [];
$options['_none'] = $this
->t('Select entity type');
$types = $this->entityTypeManager
->getDefinitions();
foreach ($types as $key => $type) {
if ($type instanceof ContentEntityType && $type
->getLinkTemplates('canocical')) {
$options[$key] = $type
->getLabel();
}
}
return $options;
}
/**
* Get list of entity bundle.
*
* @param string $entity
* Entity type.
*
* @return array
* Array of values.
*/
private function getEntityBundle($entity) {
$options = [];
$options['_none'] = $this
->t('Select entity bundle');
foreach ($this->entityTypeBundleInfo
->getBundleInfo($entity) as $key => $type) {
$options[$key] = $type['label'];
}
return $options;
}
/**
* Get all available languages.
*
* @return array
* Array of available languages.
*/
private function getAvailableLanguages() {
$options = [
'und' => $this
->t('Language not specified'),
];
$langs = $this->languageManager
->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
foreach ($langs as $key => $lang) {
$options[$key] = $lang
->getName();
}
return $options;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CustomBreadcrumbsForm:: |
protected | property | EntityTypeBundleInfo. | |
CustomBreadcrumbsForm:: |
protected | property | Language interface. | |
CustomBreadcrumbsForm:: |
public static | function | Ajax callback for the entity bundle dependent configuration options. | |
CustomBreadcrumbsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
CustomBreadcrumbsForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
CustomBreadcrumbsForm:: |
private | function | Get all available languages. | |
CustomBreadcrumbsForm:: |
private | function | Get list of entity bundle. | |
CustomBreadcrumbsForm:: |
private | function | Get all available content entity types. | |
CustomBreadcrumbsForm:: |
public static | function | Get token type by entity. | |
CustomBreadcrumbsForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
CustomBreadcrumbsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
CustomBreadcrumbsForm:: |
public | function | CustomBreadcrumbsForm constructor. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 11 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 35 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
13 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties. | 9 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
6 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
12 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
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 FormInterface:: |
20 |
FormBase:: |
protected | property | The config factory. | 3 |
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. | 3 |
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. | |
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. | |
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. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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. | 4 |
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. |