Skip to content

Instantly share code, notes, and snippets.

@superbiche
Last active March 6, 2019 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superbiche/a2df107d5a2396aab5f5eb17f64375c3 to your computer and use it in GitHub Desktop.
Save superbiche/a2df107d5a2396aab5f5eb17f64375c3 to your computer and use it in GitHub Desktop.
diff --git a/config/schema/taxonomy_menu.schema.yml b/config/schema/taxonomy_menu.schema.yml
index 91f73c0..a247492 100644
--- a/config/schema/taxonomy_menu.schema.yml
+++ b/config/schema/taxonomy_menu.schema.yml
@@ -20,6 +20,9 @@ taxonomy_menu.taxonomy_menu.*:
depth:
type: integer
label: 'Depth'
+ limit:
+ type: integer
+ label: 'Limit'
menu_parent:
type: string
label: 'Menu parent'
diff --git a/src/Entity/TaxonomyMenu.php b/src/Entity/TaxonomyMenu.php
index 27f2c23..1d53f24 100644
--- a/src/Entity/TaxonomyMenu.php
+++ b/src/Entity/TaxonomyMenu.php
@@ -67,6 +67,13 @@ class TaxonomyMenu extends ConfigEntityBase implements TaxonomyMenuInterface {
*/
protected $depth;
+ /**
+ * The limit number of items.
+ *
+ * @var int
+ */
+ protected $limit;
+
/**
* The menu to embed the vocabulary.
*
@@ -125,6 +132,13 @@ class TaxonomyMenu extends ConfigEntityBase implements TaxonomyMenuInterface {
return $this->menu_parent;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function getLimit() {
+ return $this->limit;
+ }
+
/**
* Return if menu items should be ordered by the terms weight.
*
@@ -202,6 +216,10 @@ class TaxonomyMenu extends ConfigEntityBase implements TaxonomyMenuInterface {
$links[$mlid] = $this->buildMenuDefinition($term, $base_plugin_definition);
}
+ if ($this->limit && is_int($this->limit)) {
+ $links = array_slice($links, 0, $this->limit);
+ }
+
return $links;
}
diff --git a/src/Form/TaxonomyMenuForm.php b/src/Form/TaxonomyMenuForm.php
index 300cf08..c5abc99 100644
--- a/src/Form/TaxonomyMenuForm.php
+++ b/src/Form/TaxonomyMenuForm.php
@@ -22,14 +22,14 @@ class TaxonomyMenuForm extends EntityForm {
* @var \Drupal\Core\Menu\MenuParentFormSelector
*/
protected $menuParentSelector;
-
+
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManager
*/
protected $entityFieldManager;
-
+
/**
* Constructs a new TaxonomyMenuMenuLink.
*
@@ -151,6 +151,11 @@ class TaxonomyMenuForm extends EntityForm {
'#default_value' => $taxonomy_menu->getDepth(),
'#options' => range(1, 9),
];
+ $form['limit'] = [
+ '#type' => 'number',
+ '#title' => $this->t('Limit number of elements'),
+ '#default_value' =>$taxonomy_menu->getLimit(),
+ ];
// Menu selection.
$custom_menus = Menu::loadMultiple();
diff --git a/src/TaxonomyMenuInterface.php b/src/TaxonomyMenuInterface.php
index 11d7973..e60d347 100644
--- a/src/TaxonomyMenuInterface.php
+++ b/src/TaxonomyMenuInterface.php
@@ -34,6 +34,14 @@ interface TaxonomyMenuInterface extends ConfigEntityInterface {
*/
public function getDepth();
+ /**
+ * Get the limit of items.
+ *
+ * @return int
+ * The limit.
+ */
+ public function getLimit();
+
/**
* Get the menu item to use as the parent for the taxonomy menu.
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment