ProfileForm.php in Entity Legal 3.0.x        
                          
                  
                        
  
  
  
  
File
  src/Plugin/EntityLegal/ProfileForm.php
  
    View source  
  <?php
namespace Drupal\entity_legal\Plugin\EntityLegal;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_legal\EntityLegalPluginBase;
class ProfileForm extends EntityLegalPluginBase {
  
  public function execute(array &$context = []) {
    if (!empty($this->documents)) {
      $context['form']['actions']['submit']['#submit'][] = [
        get_class($this),
        'submitForm',
      ];
      
      foreach ($this->documents as $document) {
        $context['form']['#cache']['tags'][] = "entity_legal_document:{$document->id()}";
        $context['form']["legal_{$document->id()}"] = [
          '#type' => 'checkbox',
          '#title' => $document
            ->getAcceptanceLabel(),
          '#default_value' => $document
            ->userHasAgreed(),
          '#required' => TRUE,
        ];
        $context['form']['#entity_legal'] = $this;
      }
    }
  }
  
  public static function submitForm(array &$form, FormStateInterface $form_state) {
    
    foreach ($form['#entity_legal']->documents as $document) {
      if (!empty($form_state
        ->getValue([
        'legal_' . $document
          ->id(),
      ]))) {
        $published_version = $document
          ->getPublishedVersion();
        $acceptance = \Drupal::entityTypeManager()
          ->getStorage(ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME)
          ->create([
          'uid' => $form_state
            ->getValue('uid'),
          'document_version_name' => $published_version
            ->id(),
        ]);
        $acceptance
          ->save();
      }
    }
  }
}
 
Classes
        
  
  
      
      
         
      
                  | Name   | Description | 
    
    
          
                  | ProfileForm | Method class for displaying a checkbox on the user register form. |