Skip to content

Commit

Permalink
Merge pull request #14053 from jdalsem/4.3-features
Browse files Browse the repository at this point in the history
feat(groups): moved group tool field into its own view
  • Loading branch information
jdalsem committed Jun 21, 2022
2 parents 12d91e0 + 77ff14c commit ca6fdfd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
17 changes: 16 additions & 1 deletion engine/classes/Elgg/Groups/Tool.php
Expand Up @@ -79,7 +79,8 @@ public function getPriority() {
}

/**
* Get module title
* Get tool label
*
* @return string
*/
public function getLabel() {
Expand All @@ -91,6 +92,20 @@ public function getLabel() {
return elgg_echo("groups:tool:{$this->name}");
}

/**
* Get tool description
*
* @return string|null
*/
public function getDescription(): ?string {
$lan_key = "groups:tool:{$this->name}:description";
if (!elgg_language_key_exists($lan_key)) {
return null;
}

return elgg_echo($lan_key);
}

/**
* Is the tool enabled by default?
* @return bool
Expand Down
6 changes: 5 additions & 1 deletion engine/tests/phpunit/unit/Elgg/Groups/ToolsUnitTest.php
Expand Up @@ -36,7 +36,11 @@ public function testCanRegisterTools() {
$this->assertEquals('yes', $tool->mapMetadataValue(true));
$this->assertEquals('no', $tool->mapMetadataValue(false));
$this->assertEquals(300, $tool->priority);


$this->assertNull($tool->getDescription());
elgg()->translator->addTranslation('en', ['groups:tool:my-tool:description' => 'translated tool description']);
$this->assertEquals('translated tool description', $tool->getDescription());

elgg()->group_tools->unregister('my-tool');

$tool = elgg()->group_tools->get('my-tool');
Expand Down
26 changes: 26 additions & 0 deletions mod/groups/views/default/groups/edit/tool.php
@@ -0,0 +1,26 @@
<?php
/**
* This view displays a single group tool
*
* @uses $vars['tool'] \Elgg\Groups\Tool
* @uses $vars['entity'] \ElggGroup
* @uses $vars['value'] (optional) tool value
* @uses $vars['class'] (optional) field class
*/

$tool = elgg_extract('tool', $vars);
if (!$tool instanceof \Elgg\Groups\Tool) {
return;
}

echo elgg_view_field([
'#type' => 'checkbox',
'#label' => $tool->getLabel(),
'#help' => $tool->getDescription(),
'#class' => elgg_extract_class($vars),
'name' => $tool->mapMetadataName(),
'value' => 'yes',
'default' => 'no',
'switch' => true,
'checked' => elgg_extract('value', $vars) === 'yes',
]);
21 changes: 8 additions & 13 deletions mod/groups/views/default/groups/edit/tools.php
Expand Up @@ -7,30 +7,25 @@

$entity = elgg_extract('entity', $vars);

if ($entity instanceof ElggGroup) {
if ($entity instanceof \ElggGroup) {
$tools = elgg()->group_tools->group($entity);
} else {
$tools = elgg()->group_tools->all();
}

$tools = $tools->sort()->all();
/* @var $tools \Elgg\Groups\Tool[] */
$tools = $tools->sort(function (\Elgg\Groups\Tool $a, \Elgg\Groups\Tool$b) {
return strcmp($a->getLabel(), $b->getLabel());
})->all();

if (empty($tools)) {
return;
}

foreach ($tools as $tool) {
$prop_name = $tool->mapMetadataName();
$value = elgg_extract($prop_name, $vars);

echo elgg_view_field([
'#type' => 'checkbox',
'#label' => $tool->label,
'name' => $prop_name,
'value' => 'yes',
'default' => 'no',
'switch' => true,
'checked' => ($value === 'yes') ? true : false,
echo elgg_view('groups/edit/tool', [
'entity' => $entity,
'tool' => $tool,
'value' => elgg_extract($tool->mapMetadataName(), $vars),
]);
}

0 comments on commit ca6fdfd

Please sign in to comment.