turnstile

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

TurnstileConfigSubscriber.php (5199B)


      1 <?php
      2 
      3 /**
      4  * @file
      5  * Location: src/EventSubscriber/TurnstileConfigSubscriber.php
      6  *
      7  * Subscriber for turnstile config changes.
      8  */
      9 
     10 namespace Drupal\taler_turnstile\EventSubscriber;
     11 
     12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
     13 use Drupal\Core\StringTranslation\StringTranslationTrait;
     14 use Drupal\Core\StringTranslation\TranslationInterface;
     15 use Drupal\Core\Config\ConfigCrudEvent;
     16 use Drupal\Core\Config\ConfigEvents;
     17 use Drupal\Core\Entity\EntityTypeManagerInterface;
     18 use Drupal\taler_turnstile\TurnstileFieldManager;
     19 use Drupal\Core\Messenger\MessengerInterface;
     20 
     21 class TurnstileConfigSubscriber implements EventSubscriberInterface {
     22 
     23   // This provides the $this->t() method.
     24   use StringTranslationTrait;
     25 
     26   /**
     27    * The entity type manager.
     28    *
     29    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     30    */
     31   protected $entityTypeManager;
     32 
     33   /**
     34    * The Turnstile field manager.
     35    *
     36    * @var \Drupal\taler_turnstile\TurnstileFieldManager
     37    */
     38   protected $fieldManager;
     39 
     40 
     41   /**
     42    * The messenger service.
     43    *
     44    * @var \Drupal\Core\Messenger\MessengerInterface
     45    */
     46   protected $messenger;
     47 
     48   /**
     49    * Constructs a TurnstileSettingsForm object.
     50    *
     51    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
     52    *   The translation interface.
     53    * @param \Drupal\Core\Messenger\MessengerInterface
     54    *   The messenger interface.
     55    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     56    *   The entity type manager.
     57    * @param \Drupal\taler_turnstile\TurnstileFieldManager $field_manager
     58    *   The field manager.
     59    */
     60   public function __construct(TranslationInterface $string_translation,
     61                               MessengerInterface $messenger,
     62                               EntityTypeManagerInterface $entity_type_manager,
     63                               TurnstileFieldManager $field_manager) {
     64     $this->stringTranslation = $string_translation;
     65     $this->messenger = $messenger;
     66     $this->entityTypeManager = $entity_type_manager;
     67     $this->fieldManager = $field_manager;
     68   }
     69 
     70   /**
     71    * {@inheritdoc}
     72    */
     73   public static function getSubscribedEvents() {
     74     // Listen for the configuration save event.
     75     $events[ConfigEvents::SAVE][] = ['onConfigSave'];
     76     return $events;
     77   }
     78 
     79   /**
     80    * React to configuration changes.
     81    *
     82    * @param \Drupal\Core\Config\ConfigCrudEvent $event
     83    * The configuration event.
     84    */
     85   public function onConfigSave(ConfigCrudEvent $event) {
     86     $config = $event->getConfig();
     87     if ($config->getName() !== 'taler_turnstile.settings') {
     88       return;
     89     }
     90     if ($event->isChanged('enabled_content_types')) {
     91       $old_enabled_types = $config->getOriginal('enabled_content_types') ?? [];
     92       $new_enabled_types = $config->get('enabled_content_types');
     93 
     94       // FIXME: Consider validating new_enabled_types.
     95 
     96       // Find content types to add and remove.
     97       $types_to_add = array_diff($new_enabled_types, $old_enabled_types);
     98       $types_to_remove = array_diff($old_enabled_types, $new_enabled_types);
     99 
    100       // Add fields to newly enabled content types.
    101       if (!empty($types_to_add)) {
    102         $this->fieldManager->addFieldsToContentTypes($types_to_add);
    103       }
    104 
    105       // Remove fields from disabled content types.
    106       if (!empty($types_to_remove)) {
    107         $this->fieldManager->removeFieldsFromContentTypes($types_to_remove);
    108       }
    109       if (empty($new_enabled_types)) {
    110         $this->fieldManager->cleanupFieldStorage();
    111       }
    112       // Display summary of changes.
    113       if (!empty($types_to_add) || !empty($types_to_remove)) {
    114         $this->displayFieldChanges($types_to_add, $types_to_remove);
    115       }
    116       \Drupal::logger('taler_turnstile')->info('Turnstile content types changed from @old to @new.', [
    117         '@old' => json_encode($old_enabled_types),
    118         '@new' => json_encode($new_enabled_types),
    119       ]);
    120     }
    121   }
    122 
    123   /**
    124    * Display messages about field changes.
    125    *
    126    * @param array $types_added
    127    *   Content types that had fields added.
    128    * @param array $types_removed
    129    *   Content types that had fields removed.
    130    */
    131   protected function displayFieldChanges(array $types_added, array $types_removed) {
    132     $content_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
    133 
    134     if (!empty($types_added)) {
    135       $added_labels = [];
    136       foreach ($types_added as $type) {
    137         if (isset($content_types[$type])) {
    138           $added_labels[] = $content_types[$type]->label();
    139         }
    140       }
    141       if (!empty($added_labels)) {
    142         $this->messenger->addStatus(
    143           $this->t('Price category fields added to: @types', [
    144             '@types' => implode(', ', $added_labels),
    145           ])
    146         );
    147       }
    148     }
    149 
    150     if (!empty($types_removed)) {
    151       $removed_labels = [];
    152       foreach ($types_removed as $type) {
    153         if (isset($content_types[$type])) {
    154           $removed_labels[] = $content_types[$type]->label();
    155         }
    156       }
    157       if (!empty($removed_labels)) {
    158         $this->messenger->addStatus(
    159           $this->t('Price category fields removed from: @types', [
    160             '@types' => implode(', ', $removed_labels),
    161           ])
    162         );
    163       }
    164     }
    165   }
    166 }