dev/core#2061 PCP: call the hook_civicrm_links hook for PCP page user actions
authorMathieu Lutfy <mathieu@symbiotic.coop>
Wed, 23 Sep 2020 20:07:39 +0000 (16:07 -0400)
committerMathieu Lutfy <mathieu@bidon.ca>
Mon, 28 Sep 2020 20:10:43 +0000 (16:10 -0400)
CRM/PCP/BAO/PCP.php
CRM/PCP/Page/PCPInfo.php
tests/phpunit/CRM/PCP/BAO/PCPTest.php

index e71609eccd31abec138687a9913c977ea0fdd0b3..6fdd36d0275380d8eb62600bce4d631c9174aed5 100644 (file)
@@ -88,8 +88,6 @@ WHERE  civicrm_pcp.contact_id = civicrm_contact.id
    *   array of Pcp if found
    */
   public static function getPcpDashboardInfo($contactId) {
-    $links = self::pcpLinks();
-
     $query = '
 SELECT pcp.*, block.is_tellfriend_enabled FROM civicrm_pcp pcp
 LEFT JOIN civicrm_pcp_block block ON block.id = pcp.pcp_block_id
@@ -98,13 +96,13 @@ WHERE pcp.is_active = 1
 ORDER BY page_type, page_id';
 
     $params = [1 => [$contactId, 'Integer']];
-
     $pcpInfoDao = CRM_Core_DAO::executeQuery($query, $params);
-    $pcpInfo = [];
-    $hide = $mask = array_sum(array_keys($links['all']));
-    $contactPCPPages = [];
 
+    $links = self::pcpLinks();
+    $hide = $mask = array_sum(array_keys($links['all']));
     $approved = CRM_Core_PseudoConstant::getKey('CRM_PCP_BAO_PCP', 'status_id', 'Approved');
+    $contactPCPPages = [];
+    $pcpInfo = [];
 
     while ($pcpInfoDao->fetch()) {
       $mask = $hide;
@@ -264,10 +262,13 @@ WHERE pcp.id = %1 AND cc.contribution_status_id = %2 AND cc.is_test = 0";
   /**
    * Get action links.
    *
+   * @param int $pcpId
+   *   Contains the pcp ID. Defaults to NULL for backwards compatibility.
+   *
    * @return array
    *   (reference) of action links
    */
-  public static function &pcpLinks() {
+  public static function &pcpLinks($pcpId = NULL) {
     if (!(self::$_pcpLinks)) {
       $deleteExtra = ts('Are you sure you want to delete this Personal Campaign Page?') . '\n' . ts('This action cannot be undone.');
 
@@ -326,6 +327,8 @@ WHERE pcp.id = %1 AND cc.contribution_status_id = %2 AND cc.is_test = 0";
           'title' => ts('Delete'),
         ],
       ];
+
+      CRM_Utils_Hook::links('pcp.user.actions', 'Pcp', $pcpId, self::$_pcpLinks);
     }
     return self::$_pcpLinks;
   }
index 437ef4d968e9ff62da9c30ef36dc5633b8a84101..38140ba235b12993c2be79b3211648b939607eea 100644 (file)
@@ -141,7 +141,7 @@ class CRM_PCP_Page_PCPInfo extends CRM_Core_Page {
 
       $this->assign('owner', $owner);
 
-      $link = CRM_PCP_BAO_PCP::pcpLinks();
+      $link = CRM_PCP_BAO_PCP::pcpLinks($pcpInfo['id']);
 
       $hints = [
         CRM_Core_Action::UPDATE => ts('Change the content and appearance of your page'),
index d03f933036dc0f44714edf39d21459145a38ee50..adfe01f2bae43904014480a5cabe65d74c73b9b5 100644 (file)
@@ -123,6 +123,52 @@ class CRM_PCP_BAO_PCPTest extends CiviUnitTestCase {
     ], CRM_PCP_BAO_PCP::getPcpDashboardInfo($contactID));
   }
 
+  /**
+   * Test that hook_civicrm_links is called.
+   */
+  public function testPcpInfoLinksHook() {
+    Civi::dispatcher()->addListener('hook_civicrm_links', [$this, 'hookLinks']);
+
+    // Reset the cache otherwise our hook will not be called
+    CRM_PCP_BAO_PCP::$_pcpLinks = NULL;
+
+    $block = CRM_PCP_BAO_PCPBlock::create($this->pcpBlockParams());
+    $contactID = $this->individualCreate();
+    $contributionPage = $this->callAPISuccessGetSingle('ContributionPage', []);
+    $pcp = $this->callAPISuccess('Pcp', 'create', ['contact_id' => $contactID, 'title' => 'pcp', 'page_id' => $contributionPage['id'], 'pcp_block_id' => $block->id, 'is_active' => TRUE, 'status_id' => 'Approved']);
+
+    $links = CRM_PCP_BAO_PCP::pcplinks($pcp['id']);
+
+    foreach ($links['all'] as $link) {
+      if ($link['name'] == 'URL for this Page') {
+        $found = TRUE;
+        $this->assertEquals($link['url'], 'https://civicrm.org/mih');
+      }
+    }
+
+    $this->assertEquals($found, TRUE);
+  }
+
+  /**
+   * This is the listener for hook_civicrm_links
+   *
+   * Replaces the "URL for this Page" link by a hardcoded link.
+   * This is the listener for hook_civicrm_alterReportVar
+   *
+   * @param \Civi\Core\Event\GenericHookEvent $e
+   *   Should contain 'op', 'links', and other members corresponding
+   *   to the hook parameters.
+   */
+  public function hookLinks(\Civi\Core\Event\GenericHookEvent $e) {
+    if ($e->op == 'pcp.user.actions') {
+      foreach ($e->links['all'] as $key => &$link) {
+        if ($link['name'] == 'URL for this Page') {
+          $e->links['all'][$key]['url'] = 'https://civicrm.org/mih';
+        }
+      }
+    }
+  }
+
   /**
    * Test that CRM_Contribute_BAO_Contribution::_gatherMessageValues() works
    * with PCP.