Skip to content

Commit

Permalink
fix: Do not handle node add attribute form when no attribute exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 30, 2023
1 parent 9be3f07 commit db77e62
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
11 changes: 10 additions & 1 deletion lib/Rozier/src/Controllers/Nodes/NodesAttributesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Themes\Rozier\Controllers\Nodes;

use RZ\Roadiz\CoreBundle\Entity\Attribute;
use RZ\Roadiz\CoreBundle\Entity\AttributeValue;
use RZ\Roadiz\CoreBundle\Entity\AttributeValueTranslation;
use RZ\Roadiz\CoreBundle\Entity\Node;
Expand Down Expand Up @@ -148,6 +149,11 @@ public function editAction(Request $request, int $nodeId, int $translationId): R
return $this->render('@RoadizRozier/nodes/attributes/edit.html.twig', $this->assignation);
}

protected function hasAttributes(): bool
{
return $this->em()->getRepository(Attribute::class)->countBy([]) > 0;
}

/**
* @param Request $request
* @param Node $node
Expand All @@ -157,6 +163,9 @@ public function editAction(Request $request, int $nodeId, int $translationId): R
*/
protected function handleAddAttributeForm(Request $request, Node $node, Translation $translation): ?RedirectResponse
{
if (!$this->hasAttributes()) {
return null;
}
$attributeValue = new AttributeValue();
$attributeValue->setAttributable($node);
$addAttributeForm = $this->createForm(AttributeValueType::class, $attributeValue, [
Expand Down Expand Up @@ -185,7 +194,7 @@ protected function handleAddAttributeForm(Request $request, Node $node, Translat
* @param int $translationId
* @param int $attributeValueId
*
* @return RedirectResponse|Response
* @return Response
*/
public function deleteAction(Request $request, $nodeId, $translationId, $attributeValueId): Response
{
Expand Down
42 changes: 22 additions & 20 deletions lib/Rozier/src/Resources/views/nodes/attributes/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,28 @@
</tbody>
</table>

<table>
<tr>
<td>
{% form_theme addAttributeForm '@RoadizRozier/horizontalForms.html.twig' %}
{{ form_start(addAttributeForm, {
'attr': {
'id': 'add-node-attribute-form',
'class': 'uk-form uk-form-stacked'
}
}) }}{{ form_widget(addAttributeForm) }}
{% apply spaceless %}
<button class="uk-button uk-button-primary" type="submit">
<span class="icon-container"><i class="uk-icon-rz-plus"></i></span>
<span class="label"><span class="label-text">{% trans %}add.a.node.attribute{% endtrans %}</span></span>
</button>
{% endapply %}
{{ form_end(addAttributeForm) }}
</td>
</tr>
</table>
{% if addAttributeForm %}
<table>
<tr>
<td>
{% form_theme addAttributeForm '@RoadizRozier/horizontalForms.html.twig' %}
{{ form_start(addAttributeForm, {
'attr': {
'id': 'add-node-attribute-form',
'class': 'uk-form uk-form-stacked'
}
}) }}{{ form_widget(addAttributeForm) }}
{% apply spaceless %}
<button class="uk-button uk-button-primary" type="submit">
<span class="icon-container"><i class="uk-icon-rz-plus"></i></span>
<span class="label"><span class="label-text">{% trans %}add.a.node.attribute{% endtrans %}</span></span>
</button>
{% endapply %}
{{ form_end(addAttributeForm) }}
</td>
</tr>
</table>
{% endif %}
</div>
<hr>

Expand Down

0 comments on commit db77e62

Please sign in to comment.