commit ca2cbc3f4f037efc627bb4547fd70715d4683abf
parent b1d1c62da0ae83a8c8ee21ffb75b4022d0c3e509
Author: Christian Grothoff <christian@grothoff.org>
Date: Sat, 11 Oct 2025 16:00:57 +0200
rename price category to turnstile price category
Diffstat:
11 files changed, 163 insertions(+), 172 deletions(-)
diff --git a/README.md b/README.md
@@ -63,7 +63,7 @@ turnstile/
│ └── turnstile.settings.yml
├── src/
│ ├── Entity/
-│ │ └── PriceCategory.php - Main entity class for PriceCategories
+│ │ └── TurnstilePriceCategory.php - Main entity class for price categories
│ ├── Form/
│ │ ├── PriceCategoryForm.php - Add/edit form handler
│ │ ├── PriceCategoryDeleteForm.php - Delete confirmation form
diff --git a/src/Entity/PriceCategory.php b/src/Entity/PriceCategory.php
@@ -1,125 +0,0 @@
-<?php
-
-/**
- * @file
- * Price category structure for the turnstile module.
- */
-
-namespace Drupal\turnstile\Entity;
-
-use Drupal\Core\Config\Entity\ConfigEntityBase;
-
-/**
- * Defines the Price Category entity.
- *
- * @ConfigEntityType(
- * id = "price_category",
- * label = @Translation("Price Category"),
- * handlers = {
- * "list_builder" = "Drupal\turnstile\PriceCategoryListBuilder",
- * "form" = {
- * "add" = "Drupal\turnstile\Form\PriceCategoryForm",
- * "edit" = "Drupal\turnstile\Form\PriceCategoryForm",
- * "delete" = "Drupal\turnstile\Form\PriceCategoryDeleteForm"
- * }
- * },
- * config_prefix = "price_category",
- * admin_permission = "administer price categories",
- * entity_keys = {
- * "id" = "id",
- * "label" = "label"
- * },
- * links = {
- * "collection" = "/admin/structure/price-categories",
- * "edit-form" = "/admin/structure/price-categories/{price_category}/edit",
- * "delete-form" = "/admin/structure/price-categories/{price_category}/delete"
- * },
- * config_export = {
- * "id",
- * "label",
- * "description",
- * "prices"
- * }
- * )
- */
-class PriceCategory extends ConfigEntityBase {
-
- /**
- * The price category ID.
- *
- * @var string
- */
- protected $id;
-
- /**
- * The price category label.
- *
- * @var string
- */
- protected $label;
-
- /**
- * The price category description.
- *
- * @var string
- */
- protected $description;
-
- /**
- * The prices array.
- *
- * Structure: ['subscription_id' => ['currency_code' => 'price']]
- *
- * @var array
- */
- protected $prices = [];
-
- /**
- * Gets the description.
- *
- * @return string
- * The description.
- */
- public function getDescription() {
- return $this->description;
- }
-
- /**
- * Gets all prices.
- *
- * @return array
- * The prices array.
- */
- public function getPrices() {
- return $this->prices ?: [];
- }
-
- /**
- * Gets the price for a specific subscription and currency.
- *
- * @param string $subscription_id
- * The subscription ID.
- * @param string $currency_code
- * The currency code.
- *
- * @return string|null
- * The price or NULL if not set.
- */
- public function getPrice($subscription_id, $currency_code) {
- return $this->prices[$subscription_id][$currency_code] ?? NULL;
- }
-
- /**
- * Sets the prices array.
- *
- * @param array $prices
- * The prices array.
- *
- * @return $this
- */
- public function setPrices(array $prices) {
- $this->prices = $prices;
- return $this;
- }
-
-}
-\ No newline at end of file
diff --git a/src/Entity/TurnstilePriceCategory.php b/src/Entity/TurnstilePriceCategory.php
@@ -0,0 +1,125 @@
+<?php
+
+/**
+ * @file
+ * Price category structure for the turnstile module.
+ */
+
+namespace Drupal\turnstile\Entity;
+
+use Drupal\Core\Config\Entity\ConfigEntityBase;
+
+/**
+ * Defines the Price Category entity.
+ *
+ * @ConfigEntityType(
+ * id = "turnstile_price_category",
+ * label = @Translation("Price Category"),
+ * handlers = {
+ * "list_builder" = "Drupal\turnstile\PriceCategoryListBuilder",
+ * "form" = {
+ * "add" = "Drupal\turnstile\Form\PriceCategoryForm",
+ * "edit" = "Drupal\turnstile\Form\PriceCategoryForm",
+ * "delete" = "Drupal\turnstile\Form\PriceCategoryDeleteForm"
+ * }
+ * },
+ * config_prefix = "turnstile_price_category",
+ * admin_permission = "administer price categories",
+ * entity_keys = {
+ * "id" = "id",
+ * "label" = "label"
+ * },
+ * links = {
+ * "collection" = "/admin/structure/price-categories",
+ * "edit-form" = "/admin/structure/price-categories/{price_category}/edit",
+ * "delete-form" = "/admin/structure/price-categories/{price_category}/delete"
+ * },
+ * config_export = {
+ * "id",
+ * "label",
+ * "description",
+ * "prices"
+ * }
+ * )
+ */
+class TurnstilePriceCategory extends ConfigEntityBase {
+
+ /**
+ * The price category ID.
+ *
+ * @var string
+ */
+ protected $id;
+
+ /**
+ * The price category label.
+ *
+ * @var string
+ */
+ protected $label;
+
+ /**
+ * The price category description.
+ *
+ * @var string
+ */
+ protected $description;
+
+ /**
+ * The prices array.
+ *
+ * Structure: ['subscription_id' => ['currency_code' => 'price']]
+ *
+ * @var array
+ */
+ protected $prices = [];
+
+ /**
+ * Gets the description.
+ *
+ * @return string
+ * The description.
+ */
+ public function getDescription() {
+ return $this->description;
+ }
+
+ /**
+ * Gets all prices.
+ *
+ * @return array
+ * The prices array.
+ */
+ public function getPrices() {
+ return $this->prices ?: [];
+ }
+
+ /**
+ * Gets the price for a specific subscription and currency.
+ *
+ * @param string $subscription_id
+ * The subscription ID.
+ * @param string $currency_code
+ * The currency code.
+ *
+ * @return string|null
+ * The price or NULL if not set.
+ */
+ public function getPrice($subscription_id, $currency_code) {
+ return $this->prices[$subscription_id][$currency_code] ?? NULL;
+ }
+
+ /**
+ * Sets the prices array.
+ *
+ * @param array $prices
+ * The prices array.
+ *
+ * @return $this
+ */
+ public function setPrices(array $prices) {
+ $this->prices = $prices;
+ return $this;
+ }
+
+}
+\ No newline at end of file
diff --git a/src/Form/PriceCategoryDeleteForm.php b/src/Form/PriceCategoryDeleteForm.php
@@ -31,7 +31,7 @@ class PriceCategoryDeleteForm extends EntityConfirmFormBase {
* {@inheritdoc}
*/
public function getCancelUrl() {
- return new Url('entity.price_category.collection');
+ return new Url('entity.turnstile_price_category.collection');
}
/**
diff --git a/src/Form/PriceCategoryForm.php b/src/Form/PriceCategoryForm.php
@@ -65,7 +65,7 @@ class PriceCategoryForm extends EntityForm {
'#type' => 'machine_name',
'#default_value' => $price_category->id(),
'#machine_name' => [
- 'exists' => '\Drupal\turnstile\Entity\PriceCategory::load',
+ 'exists' => '\Drupal\turnstile\Entity\TurnstilePriceCategory::load',
],
'#disabled' => !$price_category->isNew(),
];
diff --git a/src/Form/TurnstileSettingsForm.php b/src/Form/TurnstileSettingsForm.php
@@ -298,9 +298,9 @@ class TurnstileSettingsForm extends ConfigFormBase {
*/
protected function addFieldsToContentTypes(array $bundles) {
// Ensure field storage exists.
- $field_storage = FieldStorageConfig::loadByName('node', 'field_price');
- if (!$field_storage) {
- $field_storage = FieldStorageConfig::create([
+ $field_price_storage = FieldStorageConfig::loadByName('node', 'field_price');
+ if (!$field_price_storage) {
+ $field_price_storage = FieldStorageConfig::create([
'field_name' => 'field_price',
'entity_type' => 'node',
'type' => 'string',
@@ -311,26 +311,24 @@ class TurnstileSettingsForm extends ConfigFormBase {
'is_ascii' => TRUE,
],
]);
- $field_storage->save();
+ $field_price_storage->save();
}
-if (FALSE) {
// FIXME: code duplication with turnstile.install!
- $field_storage = FieldStorageConfig::loadByName('node', 'field_turnstile_price_category');
- if (!$field_storage) {
- $field_storage = FieldStorageConfig::create([
+ $field_category_storage = FieldStorageConfig::loadByName('node', 'field_turnstile_price_category');
+ if (!$field_category_storage) {
+ $field_category_storage = FieldStorageConfig::create([
'field_name' => 'field_turnstile_price_category',
'entity_type' => 'node',
- 'type' => 'entity_reference',
+ 'type' => 'string', // FIXME: bad type!
'module' => 'core', // FIXME: should this be system?
'cardinality' => 1,
'settings' => [
'max_length' => 255,
],
]);
- $field_storage->save();
+ $field_category_storage->save();
}
-}
foreach ($bundles as $bundle) {
// Verify content type exists.
@@ -353,7 +351,7 @@ if (FALSE) {
{
// Create field configuration.
$field_config = FieldConfig::create([
- 'field_storage' => $field_storage,
+ 'field_storage' => $field_price_storage,
'bundle' => $bundle,
'label' => 'Price',
'description' => 'Price for accessing this content (e.g., "EUR:5" or "USD:5,CHF:3.50" to support payment in multiple currencies)',
@@ -390,7 +388,6 @@ if (FALSE) {
} /* end field_price did not exist */
-if (FALSE) {
// Check if field already exists for this bundle.
$existing_field = FieldConfig::loadByName('node', $bundle, 'field_turnstile_price_category');
if ($existing_field) {
@@ -402,6 +399,7 @@ if (FALSE) {
$field_config = FieldConfig::create([
'field_name' => 'field_turnstile_price_category',
'entity_type' => 'node',
+ 'field_storage' => $field_category_storage,
'bundle' => $bundle,
'label' => t('Price Category'),
'description' => t('Select a price category for this content.'),
@@ -442,7 +440,6 @@ if (FALSE) {
$view_display->save();
}
} // end field_turnstile_price_category did not exist
-}
} // for each bundle
@@ -460,20 +457,16 @@ if (FALSE) {
if ($field_config) {
$field_config->delete();
}
-if (FALSE) {
$field_config = FieldConfig::loadByName('node', $bundle, 'field_turnstile_price_category');
if ($field_config) {
$field_config->delete();
}
-}
// Remove from form display.
$form_display = EntityFormDisplay::load('node.' . $bundle . '.default');
if ($form_display) {
$form_display->removeComponent('field_price');
-if (FALSE) {
$form_display->removeComponent('field_turnstile_price_category');
-}
$form_display->save();
}
@@ -481,9 +474,7 @@ if (FALSE) {
$view_display = EntityViewDisplay::load('node.' . $bundle . '.default');
if ($view_display) {
$view_display->removeComponent('field_price');
-if (FALSE) {
$view_display->removeComponent('field_turnstile_price_category');
-}
$view_display->save();
}
}
@@ -496,35 +487,35 @@ if (FALSE) {
* Clean up field storage if no content types are using it.
*/
protected function cleanupFieldStorage() {
- $field_storage = FieldStorageConfig::loadByName('node', 'field_price');
- if ($field_storage) {
+ $field_price_storage = FieldStorageConfig::loadByName('node', 'field_price');
+ if ($field_price_storage) {
// Get all field configs that use this storage.
$field_configs = $this->entityTypeManager
->getStorage('field_config')
->loadByProperties([
- 'field_storage' => $field_storage,
+ 'field_storage' => $field_price_storage,
]);
// If no field configs exist, delete the storage.
if (empty($field_configs)) {
- $field_storage->delete();
+ $field_price_storage->delete();
}
}
- $field_storage = FieldStorageConfig::loadByName('node', 'field_turnstile_price_category');
- if ($field_storage) {
+ $field_category_storage = FieldStorageConfig::loadByName('node', 'field_turnstile_price_category');
+ if ($field_category_storage) {
// Get all field configs that use this storage.
$field_configs = $this->entityTypeManager
->getStorage('field_config')
->loadByProperties([
- 'field_storage' => $field_storage,
+ 'field_storage' => $field_category_storage,
]);
// If no field configs exist, delete the storage.
if (empty($field_configs)) {
- $field_storage->delete();
+ $field_category_storage->delete();
}
}
}
diff --git a/src/PriceCategoryListBuilder.php b/src/PriceCategoryListBuilder.php
@@ -31,7 +31,7 @@ class PriceCategoryListBuilder extends ConfigEntityListBuilder {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
- /** @var \Drupal\turnstile\Entity\PriceCategory $entity */
+ /** @var \Drupal\turnstile\Entity\TurnstilePriceCategory $entity */
$row['label'] = $entity->label();
$row['id'] = $entity->id();
$row['description'] = $entity->getDescription();
diff --git a/turnstile.install b/turnstile.install
@@ -18,7 +18,7 @@ function turnstile_install() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_turnstile_price_category',
'entity_type' => 'node',
- 'type' => 'entity_reference',
+ 'type' => 'string', // FIXME: bad type...
'module' => 'core', // FIXME: should this be system?
'cardinality' => 1,
'settings' => [
diff --git a/turnstile.links.action.yml b/turnstile.links.action.yml
@@ -1,7 +1,7 @@
# Action links for adding new price categories.
-entity.price_category.add_form:
- route_name: entity.price_category.add_form
+entity.turnstile_price_category.add_form:
+ route_name: entity.turnstile_price_category.add_form
title: 'Add price category'
appears_on:
- - entity.price_category.collection
+ - entity.turnstile_price_category.collection
diff --git a/turnstile.links.menu.yml b/turnstile.links.menu.yml
@@ -5,9 +5,9 @@ turnstile.settings:
route_name: turnstile.settings
weight: 99
-turnstile.price_category.collection:
+turnstile.turnstile_price_category.collection:
title: 'Price Categories'
- route_name: entity.price_category.collection
+ route_name: entity.turnstile_price_category.collection
description: 'Manage price categories for content.'
parent: system.admin_structure
weight: 10
diff --git a/turnstile.routing.yml b/turnstile.routing.yml
@@ -9,34 +9,34 @@ turnstile.settings:
_admin_route: TRUE
# Routes for price categories.
-entity.price_category.collection:
+entity.turnstile_price_category.collection:
path: '/admin/structure/price-categories'
defaults:
- _entity_list: 'price_category'
+ _entity_list: 'turnstile_price_category'
_title: 'Price Categories'
requirements:
_permission: 'administer price categories'
-entity.price_category.add_form:
+entity.turnstile_price_category.add_form:
path: '/admin/structure/price-categories/add'
defaults:
- _entity_form: 'price_category.add'
+ _entity_form: 'turnstile_price_category.add'
_title: 'Add price category'
requirements:
_permission: 'administer price categories'
-entity.price_category.edit_form:
+entity.turnstile_price_category.edit_form:
path: '/admin/structure/price-categories/{price_category}/edit'
defaults:
- _entity_form: 'price_category.edit'
+ _entity_form: 'turnstile_price_category.edit'
_title: 'Edit price category'
requirements:
_permission: 'administer price categories'
-entity.price_category.delete_form:
+entity.turnstile_price_category.delete_form:
path: '/admin/structure/price-categories/{price_category}/delete'
defaults:
- _entity_form: 'price_category.delete'
+ _entity_form: 'turnstile_price_category.delete'
_title: 'Delete price category'
requirements:
_permission: 'administer price categories'