From 20b84172fc8e2ada88bc766763d4c3895d3065bc Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Fri, 6 Oct 2023 14:58:55 +0200
Subject: [PATCH 1/2] fix: I-7629 DruplanCalendarActionButtons add
 create-material button

---
 config/schema/druplan.schema.yml              |  6 +-
 .../area/DruplanCalendarActionButtons.php     | 93 +++++++++++--------
 2 files changed, 60 insertions(+), 39 deletions(-)

diff --git a/config/schema/druplan.schema.yml b/config/schema/druplan.schema.yml
index 39e109d..0f47c0f 100644
--- a/config/schema/druplan.schema.yml
+++ b/config/schema/druplan.schema.yml
@@ -10,7 +10,7 @@ views.field.druplan_date_range_all_day:
 
 views.filter.resource_planning_event_or_subevent_verf_calendar_filter:
   type: views.filter.verf
-  
+
 views.area.druplan_actions:
   type: views_area
   label: 'Text'
@@ -21,4 +21,6 @@ views.area.druplan_actions:
     create_project:
       type: boolean
       label: 'Should show the create project link'
-
+    create_material:
+      type: boolean
+      label: 'Should show the create material link'
diff --git a/src/Plugin/views/area/DruplanCalendarActionButtons.php b/src/Plugin/views/area/DruplanCalendarActionButtons.php
index 5635f3b..0729f17 100644
--- a/src/Plugin/views/area/DruplanCalendarActionButtons.php
+++ b/src/Plugin/views/area/DruplanCalendarActionButtons.php
@@ -37,6 +37,7 @@ class DruplanCalendarActionButtons extends AreaPluginBase {
     $options = parent::defineOptions();
     $options['create_event'] = ['default' => TRUE];
     $options['create_project'] = ['default' => TRUE];
+    $options['create_material'] = ['default' => TRUE];
     return $options;
   }
 
@@ -55,7 +56,50 @@ class DruplanCalendarActionButtons extends AreaPluginBase {
       '#type' => 'checkbox',
       '#title' => $this->t('Create project button.'),
       '#default_value' => $this->options['create_project'] ?? TRUE,
-    ];    
+    ];
+    $form['create_material'] = [
+      '#type' => 'checkbox',
+      '#title' => $this->t('Create material button.'),
+      '#default_value' => $this->options['create_material'] ?? TRUE,
+    ];
+
+  }
+
+  private function getButtons() {
+    return [
+      'create_event' => [
+        'title' => $this->t('Create event'),
+        'url' => [
+          'druplan.plan_project_form',
+          [],
+          [
+            'query' =>  [
+              'destination' => \Drupal::request()->getRequestUri(),
+            ],
+          ]
+        ],
+      ],
+      'create_project' => [
+        'title' => $this->t('Create project'),
+        'url' => [
+          'node.add',
+          [
+            'node_type' => 'project',
+          ],
+          []
+        ],
+      ],
+      'create_material' => [
+        'title' => $this->t('Create material'),
+        'url' => [
+          'node.add',
+          [
+            'node_type' => 'equipment',
+          ],
+          []
+        ],
+      ],
+    ];
   }
 
   /**
@@ -67,45 +111,20 @@ class DruplanCalendarActionButtons extends AreaPluginBase {
             'contexts' => ['user.roles'],
         ]
     ];
-    
-    $event_url = Url::fromRoute(
-        'druplan.plan_project_form',
-        [],
-        [
-            'query' =>  [
-                'destination' => \Drupal::request()->getRequestUri(),
-            ],
-        ]
-    );
-    if ($this->options['create_event'] && $event_url->access(\Drupal::currentUser())) {
-        $render[] = [
-            '#title' => $this->t('Create event'),
-            '#type' => 'link',
-            '#attributes' => [
-                'class' => 'button',
-            ],
-            '#url' => $event_url,
-        ];
-    }
-    
-    $project_url = Url::fromRoute(
-        'node.add',
-        [
-            'node_type' => 'project',
-        ],
-        []                
-    );
-    if ($this->options['create_project'] && $project_url->access(\Drupal::currentUser())) {    
+
+    foreach ($this->getButtons() as $key => $options) {
+      $url = Url::fromRoute(...$options['url']);
+      if (!empty($this->options[$key]) && $url->access(\Drupal::currentUser())) {
         $render[] = [
-            '#title' => $this->t('Create project'),
-            '#type' => 'link',
-            '#attributes' => [
-                'class' => 'button',
-            ],
-            '#url' => $project_url,
+          '#title' => $options['title'],
+          '#type' => 'link',
+          '#attributes' => [
+            'class' => 'button',
+          ],
+          '#url' => $url,
         ];
+      }
     }
-
     return $render;
   }
 
-- 
GitLab


From 8d5d50fcacdbd7ccc19bfec54229bc9e9b10a5e8 Mon Sep 17 00:00:00 2001
From: s j <sj@1729.be>
Date: Fri, 6 Oct 2023 15:27:58 +0200
Subject: [PATCH 2/2] fix: I-7629 DruplanCalendarActionButtons add project
 query param if on entity.node.canonical

---
 .../views/area/DruplanCalendarActionButtons.php   | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/Plugin/views/area/DruplanCalendarActionButtons.php b/src/Plugin/views/area/DruplanCalendarActionButtons.php
index 0729f17..9c2977d 100644
--- a/src/Plugin/views/area/DruplanCalendarActionButtons.php
+++ b/src/Plugin/views/area/DruplanCalendarActionButtons.php
@@ -66,7 +66,7 @@ class DruplanCalendarActionButtons extends AreaPluginBase {
   }
 
   private function getButtons() {
-    return [
+    $buttons = [
       'create_event' => [
         'title' => $this->t('Create event'),
         'url' => [
@@ -100,6 +100,13 @@ class DruplanCalendarActionButtons extends AreaPluginBase {
         ],
       ],
     ];
+
+    $route_match = \Drupal::routeMatch();
+    if ($route_match->getRouteName() == 'entity.node.canonical') {
+      $buttons['create_event']['url'][2]['query']['edit[project_id][widget][0][target_id]'] = $route_match->getParameter('node')->id();
+    }
+
+    return $buttons;
   }
 
   /**
@@ -107,9 +114,9 @@ class DruplanCalendarActionButtons extends AreaPluginBase {
    */
   public function render($empty = FALSE) {
     $render = [
-        '#cache' => [
-            'contexts' => ['user.roles'],
-        ]
+      '#cache' => [
+        'contexts' => ['user.roles', 'url.path'],
+      ]
     ];
 
     foreach ($this->getButtons() as $key => $options) {
-- 
GitLab