Skip to content

Commit

Permalink
Civi\Angular\Manager - Add hook for modifying partials
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed May 16, 2017
1 parent 81847a6 commit 2200c27
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
21 changes: 21 additions & 0 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,27 @@ public static function angularModules(&$angularModules) {
);
}

/**
* Alter the definition of some Angular HTML partials.
*
* @param \Civi\Angular\HtmlCollection $html
*
* @code
* function example_civicrm_angularPartials(HtmlCollection $html) {
* if ($doc = $html->get('~/crmMailing/EditMailingCtrl/2step.html')) {
* $doc->find('[ng-form="crmMailingSubform"]')
* ->attr('cat-stevens', 'ts(\'wild world\')');
* }
* }
* @endCode
*/
public static function angularPartials($html) {
$event = \Civi\Core\Event\GenericHookEvent::create(array(
'html' => $html,
));
Civi::dispatcher()->dispatch('hook_civicrm_angularPartials', $event);
}

/**
* This hook is called whenever the system builds a new copy of
* semi-static asset.
Expand Down
37 changes: 28 additions & 9 deletions Civi/Angular/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ protected function resolvePatterns($modules) {
}

/**
* Get the partial HTML documents for a module.
* Get the partial HTML documents for a module (unfiltered).
*
* @param string $name
* Angular module name.
Expand All @@ -211,12 +211,7 @@ protected function resolvePatterns($modules) {
* @throws \Exception
* Invalid partials configuration.
*/
public function getPartials($name) {
$cacheKey = "angular-partials::$name";
$cacheValue = $this->cache->get($cacheKey);
if ($cacheValue !== NULL) {
return $cacheValue;
}
public function getRawPartials($name) {
$module = $this->getModule($name);
$result = array();
if (isset($module['partials'])) {
Expand All @@ -228,12 +223,36 @@ public function getPartials($name) {
$result[$filename] = file_get_contents($partialDir . '/' . $file);
}
}
return $result;
}

$this->cache->set($cacheKey, $result);
return $result;
}

/**
* Get the partial HTML documents for a module.
*
* @param string $name
* Angular module name.
* @return array
* Array(string $extFilePath => string $html)
* @throws \Exception
* Invalid partials configuration.
*/
public function getPartials($name) {
$cacheKey = "angular-partials::$name";
$cacheValue = $this->cache->get($cacheKey);
if ($cacheValue !== NULL) {
return $cacheValue;
}

$html = new HtmlCollection($this->getRawPartials($name));
\CRM_Utils_Hook::angularPartials($html);
$cacheValue = $html->getStrings();

$this->cache->set($cacheKey, $cacheValue);
return $cacheValue;
}

/**
* Get list of translated strings for a module.
*
Expand Down
29 changes: 28 additions & 1 deletion tests/phpunit/Civi/Angular/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ public function testGetModules() {
*/
public function testGetPartials() {
$partials = $this->angular->getPartials('crmMailing');
$this->assertRegExp('/ng-form="crmMailing/', $partials['~/crmMailing/EditMailingCtrl/2step.html']);
$this->assertRegExp('/ng-form="crmMailingSubform">/', $partials['~/crmMailing/EditMailingCtrl/2step.html']);
// If crmMailing changes, feel free to use a different example.
}

/**
* Get HTML fragments from an example module. The HTML is modified via hook.
*/
public function testGetPartials_Hooked() {
\CRM_Utils_Hook::singleton()->setHook('civicrm_angularPartials', array($this, 'hook_civicrm_angularPartials'));
$partials = $this->angular->getPartials('crmMailing');
$this->assertRegExp('/ng-form="crmMailingSubform" cat-stevens="ts\\(\'wild world\'\\)">/', $partials['~/crmMailing/EditMailingCtrl/2step.html']);
// If crmMailing changes, feel free to use a different example.
}

Expand All @@ -122,4 +132,21 @@ public function testGetStrings() {
// If crmMailing changes, feel free to use a different example.
}

/**
* Get a translatable string from an example module. The HTML is modified via hook.
*/
public function testGetStrings_Hooked() {
\CRM_Utils_Hook::singleton()->setHook('civicrm_angularPartials', array($this, 'hook_civicrm_angularPartials'));
$strings = $this->angular->getStrings('crmMailing');
$this->assertTrue(in_array('wild world', $strings));
// If crmMailing changes, feel free to use a different example.
}

public function hook_civicrm_angularPartials(HtmlCollection $html) {
if ($doc = $html->get('~/crmMailing/EditMailingCtrl/2step.html')) {
$doc->find('[ng-form="crmMailingSubform"]')
->attr('cat-stevens', 'ts(\'wild world\')');
}
}

}

0 comments on commit 2200c27

Please sign in to comment.