Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
Change from CP section to utility
Browse files Browse the repository at this point in the history
  • Loading branch information
niektenhoopen committed Nov 7, 2023
1 parent b0c0786 commit 945f130
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
29 changes: 27 additions & 2 deletions src/MatrixInventoryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

use Craft;
use craft\base\Plugin;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterTemplateRootsEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\helpers\UrlHelper;
use craft\services\Utilities;
use craft\web\UrlManager;
use craft\web\View;
use nthmedia\MatrixInventory\settings\Settings;
use nthmedia\MatrixInventory\utilities\MatrixInventoryUtility;
use yii\base\Event;
use yii\base\Exception;

Expand Down Expand Up @@ -38,7 +42,7 @@ class MatrixInventoryPlugin extends Plugin
/**
* @var bool
*/
public bool $hasCpSection = true;
public bool $hasCpSection = false;

/**
* @inheritdoc
Expand All @@ -55,6 +59,7 @@ public function init(): void
}
});

$this->_registerUtilities();
$this->registerCPRules();

try {
Expand All @@ -64,6 +69,26 @@ public function init(): void
}
}

/**
* Register utilities.
*/
private function _registerUtilities(): void
{
// If not a web request, bail
if (!Craft::$app->getRequest()->getIsCpRequest()) {
return;
}

// Load utilities
Event::on(
Utilities::class,
Utilities::EVENT_REGISTER_UTILITY_TYPES,
static function(RegisterComponentTypesEvent $event) {
$event->types[] = MatrixInventoryUtility::class;
}
);
}

private function registerCPRules(): void
{
Event::on(
Expand Down Expand Up @@ -92,7 +117,7 @@ public function getCpNavItem(): ?array
foreach ($this->settings->matrixFields as $matrixField) {
$item['subnav']['matrix-inventory-' . $matrixField->key] = [
'label' => $matrixField->label,
'url' => 'matrix-inventory/view/' . $matrixField->key,
'url' => UrlHelper::cpUrl('matrix-inventory/view/' . $matrixField->key),
];
}

Expand Down
4 changes: 3 additions & 1 deletion src/controllers/MatrixInventoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function actionEditEntry(int $siteId, int $id): Response

private function getMatrixDataQuery(int $fieldId, string $matrixTableName)
{
return Craft::$app->db->createCommand("
$query = Craft::$app->db->createCommand("
SELECT
matrixquery.entryId,
matrixquery.siteId,
Expand Down Expand Up @@ -128,5 +128,7 @@ private function getMatrixDataQuery(int $fieldId, string $matrixTableName)
"fieldId" => $fieldId,
"now" => (string) Carbon::now()
]);

return $query;
}
}
8 changes: 3 additions & 5 deletions src/templates/index.twig
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
{% extends "_layouts/cp" %}

{% set title = "Matrix Inventory" %}

{% set content %}
{% block content %}
<h2>Matrix Block Types</h2>
<ul style="margin-left: 20px; list-style: disc;">
{% for matrixField in matrixFields %}
<li>
<a href="{{ cpUrl() }}/matrix-inventory/view/{{ matrixField.key }}">{{ matrixField.label }}</a>
<a href="{{ cpUrl('matrix-inventory/view/' ~ matrixField.key) }}">{{ matrixField.label }}</a>
</li>
{% endfor %}
</ul>
{% endset %}
{% endblock %}
2 changes: 1 addition & 1 deletion src/templates/matrix.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{% for key, item in data[site.siteId] ?? [] %}
{% set count = item | length %}
<li>
<a href="{{ cpUrl() }}/matrix-inventory/redirect/{{ site.siteId }}/{{ item[0].entryId }}" target="_blank">{{ key }}</a> {% if count > 1 %}({{ item | length }}x){% endif %}
<a href="{{ cpUrl('matrix-inventory/redirect/' ~ site.siteId ~ '/' ~ item[0].entryId) }}" target="_blank">{{ key }}</a> {% if count > 1 %}({{ item | length }}x){% endif %}
</li>
{% endfor %}
</ul>
Expand Down
56 changes: 56 additions & 0 deletions src/utilities/MatrixInventoryUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace nthmedia\MatrixInventory\utilities;

use Craft;
use craft\base\Utility;
use nthmedia\MatrixInventory\MatrixInventoryPlugin;

class MatrixInventoryUtility extends Utility
{
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('matrix-inventory', 'Matrix Inventory');
}

/**
* @inheritdoc
*/
public static function id(): string
{
return 'matrix-inventory';
}

/**
* @inheritdoc
*/
public static function iconPath(): ?string
{
// Set the icon mask path
$iconPath = Craft::getAlias('@vendor/nthmedia/matrix-inventory/src/icon-mask.svg');

// If not a string, bail
if (!is_string($iconPath)) {
return null;
}

// Return the icon mask path
return $iconPath;
}

/**
* @inheritdoc
*/
public static function contentHtml(): string
{
return Craft::$app->getView()->renderTemplate('matrix-inventory/index', [
'matrixFields' => MatrixInventoryPlugin::getInstance()->settings->matrixFields
]);
// Render the utility template
// return Craft::$app->getView()->renderTemplate('matrix-inventory/index');
}

}

0 comments on commit 945f130

Please sign in to comment.