You are here

public function EntityLegalDocumentForm::form in Entity Legal 4.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm::form()
  2. 8 src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm::form()
  3. 3.0.x src/Form/EntityLegalDocumentForm.php \Drupal\entity_legal\Form\EntityLegalDocumentForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/EntityLegalDocumentForm.php, line 93

Class

EntityLegalDocumentForm
Base form for contact form edit forms.

Namespace

Drupal\entity_legal\Form

Code

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' => 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' => t('Title'),
      'created' => t('Created'),
      'changed' => t('Updated'),
      'operations' => 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();
      }
      $options[$version
        ->id()] = [
        'title' => Link::createFromRoute($version
          ->label(), $route_name, $route_parameters),
        'created' => $version
          ->getFormattedDate('created'),
        'changed' => $version
          ->getFormattedDate('changed'),
        'operations' => Link::createFromRoute(t('Edit'), 'entity.entity_legal_document_version.edit_form', [
          'entity_legal_document' => $this->entity
            ->id(),
          'entity_legal_document_version' => $version
            ->id(),
        ]),
      ];
    }

    // By default just show a simple overview for all entities.
    $form['versions'] = [
      '#type' => 'details',
      '#title' => t('Current version'),
      '#description' => 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' => 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' => t('New users'),
    '#description' => 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' => 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' => 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' => t('Existing users'),
    '#description' => 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' => t('Require existing users to accept this agreement'),
    '#type' => 'checkbox',
    '#default_value' => $this->entity
      ->get('require_existing'),
  ];
  $form['existing_users']['require_method'] = [
    '#title' => 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;
}