turnstile

Drupal paywall plugin
Log | Files | Refs | README | LICENSE

TurnstileFieldManager.php (5573B)


      1 <?php
      2 
      3 namespace Drupal\taler_turnstile;
      4 
      5 use Drupal\Core\Entity\EntityTypeManagerInterface;
      6 use Drupal\field\Entity\FieldStorageConfig;
      7 use Drupal\field\Entity\FieldConfig;
      8 use Drupal\Core\Entity\Entity\EntityFormDisplay;
      9 use Drupal\Core\Entity\Entity\EntityViewDisplay;
     10 use Drupal\Core\StringTranslation\StringTranslationTrait;
     11 
     12 /**
     13  * Service for managing GNU Taler Turnstile fields on content types.
     14  */
     15 class TurnstileFieldManager {
     16 
     17   /**
     18    * For i18n, gives us the t() function.
     19    */
     20   use StringTranslationTrait;
     21 
     22   /**
     23    * The entity type manager.
     24    *
     25    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     26    */
     27   protected $entityTypeManager;
     28 
     29   /**
     30    * Constructs a TurnstileFieldManager object.
     31    *
     32    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     33    *   The entity type manager.
     34    */
     35   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
     36     $this->entityTypeManager = $entity_type_manager;
     37   }
     38 
     39   /**
     40    * Add GNU Taler Turnstile fields to specified content types.
     41    *
     42    * @param array $bundles
     43    *   Array of content type machine names.
     44    */
     45   public function addFieldsToContentTypes(array $bundles) {
     46     // Ensure field storage exists.
     47     $this->ensureFieldStorageExists();
     48 
     49     foreach ($bundles as $bundle) {
     50       // Verify content type exists.
     51       if (!$this->entityTypeManager->getStorage('node_type')->load($bundle)) {
     52         continue;
     53       }
     54 
     55       $this->addPriceCategoryField($bundle);
     56     }
     57   }
     58 
     59   /**
     60    * Ensure field storage configurations exist.
     61    */
     62   protected function ensureFieldStorageExists() {
     63     // Create price category field storage if it doesn't exist.
     64     $field_category_storage = FieldStorageConfig::loadByName('node', 'field_taler_turnstile_prcat');
     65     if (!$field_category_storage) {
     66       $field_category_storage = FieldStorageConfig::create([
     67         'field_name' => 'field_taler_turnstile_prcat',
     68         'entity_type' => 'node',
     69         'type' => 'entity_reference',
     70         'cardinality' => 1,
     71         'settings' => [
     72           'target_type' => 'taler_turnstile_price_category',
     73         ],
     74       ]);
     75       $field_category_storage->save();
     76     }
     77   }
     78 
     79   /**
     80    * Add price category field to a bundle.
     81    *
     82    * @param string $bundle
     83    *   The bundle machine name.
     84    */
     85   protected function addPriceCategoryField($bundle) {
     86     $field_category_storage = FieldStorageConfig::loadByName('node', 'field_taler_turnstile_prcat');
     87 
     88     $existing_field = FieldConfig::loadByName('node', $bundle, 'field_taler_turnstile_prcat');
     89     if (!$existing_field) {
     90       // Create field configuration.
     91       $field_config = FieldConfig::create([
     92         'field_name' => 'field_taler_turnstile_prcat',
     93         'entity_type' => 'node',
     94         'field_storage' => $field_category_storage,
     95         'bundle' => $bundle,
     96         'label' => $this->t('Price category'),
     97         'description' => $this->t('Select a price category for this content.'),
     98         'required' => FALSE,
     99         'settings' => [
    100           'handler' => 'default:taler_turnstile_price_category',
    101           'handler_settings' => [
    102             'target_bundles' => NULL,
    103             'sort' => [
    104               'field' => 'label',
    105               'direction' => 'ASC',
    106             ],
    107             'auto_create' => FALSE,
    108           ],
    109         ],
    110       ]);
    111       $field_config->save();
    112 
    113       // Add to form display.
    114       $form_display = EntityFormDisplay::load('node.' . $bundle . '.default');
    115       if ($form_display) {
    116         $form_display->setComponent('field_taler_turnstile_prcat', [
    117           'type' => 'options_select',
    118           'weight' => 10,
    119           'settings' => [],
    120         ]);
    121         $form_display->save();
    122       }
    123 
    124       // Add to view display.
    125       $view_display = EntityViewDisplay::load('node.' . $bundle . '.default');
    126       if ($view_display) {
    127         $view_display->setComponent('field_taler_turnstile_prcat', [
    128           'type' => 'entity_reference_label',
    129           'weight' => 10,
    130           'label' => 'above',
    131           'settings' => [
    132             'link' => FALSE,
    133           ],
    134         ]);
    135         $view_display->save();
    136       }
    137     }
    138   }
    139 
    140   /**
    141    * Remove GNU Taler Turnstile fields from specified content types.
    142    *
    143    * @param array $bundles
    144    *   Array of content type machine names.
    145    */
    146   public function removeFieldsFromContentTypes(array $bundles) {
    147     foreach ($bundles as $bundle) {
    148       $field_config = FieldConfig::loadByName('node', $bundle, 'field_taler_turnstile_prcat');
    149       if ($field_config) {
    150         $field_config->delete();
    151       }
    152 
    153       // Remove from form display.
    154       $form_display = EntityFormDisplay::load('node.' . $bundle . '.default');
    155       if ($form_display) {
    156         $form_display->removeComponent('field_taler_turnstile_prcat');
    157         $form_display->save();
    158       }
    159 
    160       // Remove from view display.
    161       $view_display = EntityViewDisplay::load('node.' . $bundle . '.default');
    162       if ($view_display) {
    163         $view_display->removeComponent('field_taler_turnstile_prcat');
    164         $view_display->save();
    165       }
    166     }
    167   }
    168 
    169   /**
    170    * Clean up field storage when no content types are using it anymore.
    171    */
    172   public function cleanupFieldStorage() {
    173     $field_category_storage = FieldStorageConfig::loadByName('node', 'field_taler_turnstile_prcat');
    174     if ($field_category_storage) {
    175       $field_configs = $this->entityTypeManager
    176         ->getStorage('field_config')
    177         ->loadByProperties([
    178           'field_storage' => $field_category_storage,
    179         ]);
    180       $field_category_storage->delete();
    181     }
    182   }
    183 
    184 }