You are here

class EntityLegalDocumentForm in Entity Legal 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm
  2. 8 src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm
  3. 4.0.x src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm

Base form for contact form edit forms.

Hierarchy

Expanded class hierarchy of EntityLegalDocumentForm

File

src/Form/EntityLegalDocumentForm.php, line 25

Namespace

Drupal\entity_legal\Form
View source
class EntityLegalDocumentForm extends EntityForm implements ContainerInjectionInterface {
  use ConfigFormBaseTrait;

  /**
   * The path alias storage.
   *
   * @var \Drupal\Core\Entity\ContentEntityStorageInterface
   */
  protected $aliasStorage;

  /**
   * The entity legal plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $pluginManager;

  /**
   * The entity being used by this form.
   *
   * @var \Drupal\entity_legal\EntityLegalDocumentInterface
   */
  protected $entity;

  /**
   * The AccountProxy service.
   *
   * @var \Drupal\Core\Session\AccountProxy
   */
  protected $currentUser;

  /**
   * The Module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public function __construct(ContentEntityStorageInterface $alias_storage, PluginManagerInterface $plugin_manager, AccountProxy $currentUser, ModuleHandler $moduleHandler) {
    $this->aliasStorage = $alias_storage;
    $this->pluginManager = $plugin_manager;
    $this->currentUser = $currentUser;
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $etm = $container
      ->get('entity_type.manager');
    return new static($etm
      ->getStorage('path_alias'), $container
      ->get('plugin.manager.entity_legal'), $container
      ->get('current_user'), $container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['label'] = [
      '#title' => $this
        ->t('Administrative label'),
      '#type' => 'textfield',
      '#default_value' => $this->entity
        ->label(),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#title' => $this
        ->t('Machine-readable name'),
      '#required' => TRUE,
      '#default_value' => $this->entity
        ->id(),
      '#machine_name' => [
        'exists' => '\\Drupal\\entity_legal\\Entity\\EntityLegalDocument::load',
      ],
      '#disabled' => !$this->entity
        ->isNew(),
      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    ];
    if (!in_array($this->operation, [
      'add',
      'clone',
    ])) {
      $versions = $this->entity
        ->getAllVersions();
      if ($this->operation == 'edit' && empty($versions)) {
        \Drupal::messenger()
          ->addWarning(t('No versions for this document have been found. <a href=":add_link">Add a version</a> to use this document.', [
          ':add_link' => Url::fromRoute('entity.entity_legal_document_version.add_form', [
            'entity_legal_document' => $this->entity
              ->id(),
          ])
            ->toString(),
        ]));
      }
      $header = [
        'title' => $this
          ->t('Title'),
        'created' => $this
          ->t('Created'),
        'changed' => $this
          ->t('Updated'),
        'operations' => $this
          ->t('Operations'),
      ];
      $options = [];

      /** @var \Drupal\entity_legal\Entity\EntityLegalDocumentVersion $version */
      $published_version = NULL;
      foreach ($versions as $version) {
        $route_parameters = [
          'entity_legal_document' => $this->entity
            ->id(),
        ];

        // Use the default uri if this version is the current published version.
        if ($version
          ->isPublished()) {
          $published_version = $version
            ->id();
          $route_name = 'entity.entity_legal_document.canonical';
        }
        else {
          $route_name = 'entity.entity_legal_document_version.canonical';
          $route_parameters['entity_legal_document_version'] = $version
            ->id();
        }
        $links['edit'] = [
          'title' => $this
            ->t('Edit'),
          'url' => Url::fromRoute('entity.entity_legal_document_version.edit_form', [
            'entity_legal_document_version' => $version
              ->id(),
          ]),
        ];
        if ($version
          ->isTranslatable()) {
          try {
            $links['translate'] = [
              'title' => $this
                ->t('Translate'),
              'url' => $version
                ->toUrl('drupal:content-translation-overview'),
            ];
          } catch (EntityMalformedException $e) {
          }
        }
        $operations = [
          '#type' => 'operations',
          '#links' => $links,
        ];
        $options[$version
          ->id()] = [
          'title' => Link::createFromRoute($version
            ->label(), $route_name, $route_parameters),
          'created' => $version
            ->getFormattedDate('created'),
          'changed' => $version
            ->getFormattedDate('changed'),
          'operations' => render($operations),
        ];
      }

      // By default just show a simple overview for all entities.
      $form['versions'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Current version'),
        '#description' => $this
          ->t('The current version users must agree to. If requiring existing users to accept, those users will be prompted if they have not accepted this particular version in the past.'),
        '#open' => TRUE,
        '#tree' => FALSE,
      ];
      $form_state
        ->set('published_version', $published_version);
      $form['versions']['published_version'] = [
        '#type' => 'tableselect',
        '#header' => $header,
        '#options' => $options,
        '#empty' => $this
          ->t('Create a document version to set up a default'),
        '#multiple' => FALSE,
        '#default_value' => $published_version,
      ];
    }
    $form['settings'] = [
      '#type' => 'vertical_tabs',
      '#weight' => 27,
    ];
    $form['new_users'] = [
      '#title' => $this
        ->t('New users'),
      '#description' => $this
        ->t('Visit the <a href=":permissions">permissions</a> page to ensure that users can view the document.', [
        ':permissions' => Url::fromRoute('user.admin_permissions')
          ->toString(),
      ]),
      '#type' => 'details',
      '#group' => 'settings',
      '#parents' => [
        'settings',
        'new_users',
      ],
      '#tree' => TRUE,
    ];
    $form['new_users']['require'] = [
      '#title' => $this
        ->t('Require new users to accept this agreement on signup'),
      '#type' => 'checkbox',
      '#default_value' => $this->entity
        ->get('require_signup'),
    ];
    $form['new_users']['require_method'] = [
      '#title' => $this
        ->t('Present to user as'),
      '#type' => 'select',
      '#options' => $this
        ->getAcceptanceDeliveryMethodOptions('new_users'),
      '#default_value' => $this->entity
        ->getAcceptanceDeliveryMethod(TRUE),
      '#states' => [
        'visible' => [
          ':input[name="settings[new_users][require]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['existing_users'] = [
      '#title' => $this
        ->t('Existing users'),
      '#description' => $this
        ->t('Visit the <a href=":permissions">permissions</a> page to configure which existing users these settings apply to.', [
        ':permissions' => Url::fromRoute('user.admin_permissions')
          ->toString(),
      ]),
      '#type' => 'details',
      '#group' => 'settings',
      '#parents' => [
        'settings',
        'existing_users',
      ],
      '#tree' => TRUE,
    ];
    $form['existing_users']['require'] = [
      '#title' => $this
        ->t('Require existing users to accept this agreement'),
      '#type' => 'checkbox',
      '#default_value' => $this->entity
        ->get('require_existing'),
    ];
    $form['existing_users']['require_method'] = [
      '#title' => $this
        ->t('Present to user as'),
      '#type' => 'select',
      '#options' => $this
        ->getAcceptanceDeliveryMethodOptions('existing_users'),
      '#default_value' => $this->entity
        ->getAcceptanceDeliveryMethod(),
      '#states' => [
        'visible' => [
          ':input[name="settings[existing_users][require]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['title_pattern'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Title pattern'),
      '#description' => $this
        ->t("Customize how the legal document title appears on the document's main page. You can use tokens to build the title."),
      '#group' => 'settings',
    ];
    $form['title_pattern']['title_pattern'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Pattern'),
      '#default_value' => $this->entity
        ->get('settings')['title_pattern'],
      '#parents' => [
        'settings',
        'title_pattern',
      ],
      '#required' => TRUE,
    ];
    $form['title_pattern']['token_help'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'entity_legal_document',
      ],
    ];
    $this
      ->formPathSettings($form);
    return $form;
  }

  /**
   * Add path and pathauto settings to an existing legal document form.
   *
   * @param array $form
   *   The Form array.
   */
  protected function formPathSettings(array &$form) {
    if (!$this->moduleHandler
      ->moduleExists('path')) {
      return;
    }

    /** @var \Drupal\path_alias\PathAliasInterface $alias */
    $alias = $this
      ->pathAlias($this->entity
      ->language()
      ->getId());
    $aliasSource = NULL;
    if (!$alias) {
      $aliasSource = !$this->entity
        ->isNew() ? '/' . $this->entity
        ->toUrl()
        ->getInternalPath() : NULL;
    }
    $form['path'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('URL path settings'),
      '#group' => 'settings',
      '#attributes' => [
        'class' => [
          'path-form',
        ],
      ],
      '#attached' => [
        'library' => [
          'path/drupal.path',
        ],
      ],
      '#access' => $this
        ->hasAccessToPathAliases(),
      '#weight' => 5,
      '#tree' => TRUE,
      '#element_validate' => [
        PathWidget::class,
        'validateFormElement',
      ],
      '#parents' => [
        'path',
        0,
      ],
    ];
    $form['path']['langcode'] = [
      '#type' => 'language_select',
      '#title' => $this
        ->t('Language'),
      '#languages' => LanguageInterface::STATE_ALL,
      '#default_value' => $this->entity
        ->language()
        ->getId(),
    ];
    $form['path']['alias'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('URL alias'),
      '#default_value' => $alias ? $alias
        ->getAlias() : '',
      '#maxlength' => 255,
      '#description' => $this
        ->t('The alternative URL for this content. Use a relative path. For example, enter "/about" for the about page.'),
    ];
    $form['path']['pid'] = [
      '#type' => 'value',
      '#value' => $alias ? $alias
        ->id() : NULL,
    ];
    $form['path']['source'] = [
      '#type' => 'value',
      '#value' => $alias ? $alias
        ->getPath() : $aliasSource,
    ];
  }

  /**
   *
   */
  protected function pathAliasSource() : ?string {
    if ($this->entity
      ->isNew()) {
      return NULL;
    }
    return '/' . $this->entity
      ->toUrl()
      ->getInternalPath();
  }

  /**
   *
   */
  protected function pathAlias(string $lang_code) : ?PathAliasInterface {
    $path = $this
      ->pathAliasSource();
    if (!$path) {
      return NULL;
    }

    /** @var \Drupal\path_alias\PathAliasInterface[] $aliases */
    $aliases = $this->aliasStorage
      ->loadByProperties([
      'langcode' => $lang_code,
      'path' => $path,
    ]);
    return $aliases ? reset($aliases) : NULL;
  }

  /**
   *
   */
  protected function hasAccessToPathAliases() : bool {
    return $this->currentUser
      ->hasPermission('create url aliases') || $this->currentUser
      ->hasPermission('administer url aliases');
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $this
      ->saveDocument($form, $form_state)
      ->savePathAlias($form, $form_state)
      ->savePublishedVersion($form, $form_state);
  }

  /**
   * @return $this
   */
  protected function saveDocument(array $form, FormStateInterface $form_state) {
    $this->entity
      ->set('require_signup', $this->entity
      ->get('settings')['new_users']['require'])
      ->set('require_existing', $this->entity
      ->get('settings')['existing_users']['require']);
    $status = $this->entity
      ->save();
    if ($status == SAVED_NEW) {
      $form_state
        ->setRedirect('entity.entity_legal_document_version.add_form', [
        'entity_legal_document' => $this->entity
          ->id(),
      ]);
    }
    $this
      ->messenger()
      ->addStatus($this
      ->t('@type_label @label has been saved', [
      '@type_label' => $this->entity
        ->getEntityType()
        ->getLabel(),
      '@label' => $this->entity
        ->label(),
    ]));
    return $this;
  }

  /**
   * @return $this
   */
  protected function savePathAlias(array $form, FormStateInterface $form_state) {
    $values = (array) $form_state
      ->getValue([
      'path',
      '0',
    ], []);
    $langCode = $values['langcode'] ?? $this->entity
      ->language()
      ->getId();
    $path = $this
      ->pathAliasSource();
    $alias = $this
      ->pathAlias($langCode);
    $messenger = $this
      ->messenger();
    if (!$alias && !empty($values['alias'])) {
      $alias = $this->aliasStorage
        ->create([
        'langcode' => $langCode,
        'path' => $path,
        'alias' => $values['alias'],
      ]);
      $alias
        ->save();
      $messenger
        ->addStatus($this
        ->t('A new URL alias has been created'));
      return $this;
    }
    if ($alias && empty($values['alias'])) {
      $alias
        ->delete();
      $messenger
        ->addStatus($this
        ->t('URL alias %alias has been deleted', [
        '%alias' => $alias
          ->getAlias(),
      ]));
      return $this;
    }
    if ($alias && $alias
      ->getAlias() !== $values['alias']) {
      $alias
        ->setAlias($values['alias'])
        ->save();
      $messenger
        ->addStatus($this
        ->t('URL alias has ben changed to %alias', [
        '%alias' => $alias
          ->getAlias(),
      ]));
      return $this;
    }
    return $this;
  }

  /**
   * @return $this
   */
  protected function savePublishedVersion(array $form, FormStateInterface $form_state) {
    $published_version_id = $form_state
      ->getValue('published_version');
    if (!$published_version_id) {
      return $this;
    }

    // Update the published version.
    if ($form_state
      ->get('published_version') && $form_state
      ->get('published_version') !== $form_state
      ->getValue('published_version')) {
      $storage = $this->entityTypeManager
        ->getStorage(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME);

      /** @var \Drupal\entity_legal\EntityLegalDocumentVersionInterface $published_version */
      $published_version = $storage
        ->load($form_state
        ->getValue('published_version'));
      $this->entity
        ->setPublishedVersion($published_version);
    }
    return $this;
  }

  /**
   * Methods for presenting the legal document to end users.
   *
   * @param string $type
   *   The type of user, 'new_users' or 'existing_users'.
   *
   * @return array
   *   Methods available keyed by method name and title.
   */
  protected function getAcceptanceDeliveryMethodOptions($type) {
    $methods = [];
    foreach ($this->pluginManager
      ->getDefinitions() as $plugin) {
      if ($plugin['type'] == $type) {
        $methods[$plugin['id']] = $plugin['label'];
      }
    }
    return $methods;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$operation protected property The name of the current operation.
EntityForm::actions protected function Returns an array of supported actions for the current entity form. 35
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 3
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 13
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties. 9
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 6
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 3
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 12
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides FormInterface::submitForm 20
EntityLegalDocumentForm::$aliasStorage protected property The path alias storage.
EntityLegalDocumentForm::$currentUser protected property The AccountProxy service.
EntityLegalDocumentForm::$entity protected property The entity being used by this form. Overrides EntityForm::$entity
EntityLegalDocumentForm::$moduleHandler protected property The Module handler service. Overrides EntityForm::$moduleHandler
EntityLegalDocumentForm::$pluginManager protected property The entity legal plugin manager.
EntityLegalDocumentForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
EntityLegalDocumentForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
EntityLegalDocumentForm::formPathSettings protected function Add path and pathauto settings to an existing legal document form.
EntityLegalDocumentForm::getAcceptanceDeliveryMethodOptions protected function Methods for presenting the legal document to end users.
EntityLegalDocumentForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
EntityLegalDocumentForm::hasAccessToPathAliases protected function
EntityLegalDocumentForm::pathAlias protected function
EntityLegalDocumentForm::pathAliasSource protected function
EntityLegalDocumentForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
EntityLegalDocumentForm::saveDocument protected function
EntityLegalDocumentForm::savePathAlias protected function
EntityLegalDocumentForm::savePublishedVersion protected function
EntityLegalDocumentForm::__construct public function
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.