You are here

public function EventTypeAccessDefaultsForm::buildForm in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/EventTypeAccessDefaultsForm.php \Drupal\rng\Form\EventTypeAccessDefaultsForm::buildForm()
  2. 8 src/Form/EventTypeAccessDefaultsForm.php \Drupal\rng\Form\EventTypeAccessDefaultsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/EventTypeAccessDefaultsForm.php, line 102

Class

EventTypeAccessDefaultsForm
Form for event type access defaults.

Namespace

Drupal\rng\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $form['table'] = [
    '#type' => 'table',
    '#empty' => $this
      ->t('No access rules.'),
  ];

  /** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
  $event_type = $this->entity;
  $trigger = 'rng_event.register';

  // Header.
  $form['table']['head'] = [
    '#attributes' => [],
    [
      '#wrapper_attributes' => [
        'header' => TRUE,
        'rowspan' => 2,
      ],
      '#plain_text' => $this
        ->t('Rule'),
    ],
    [
      '#wrapper_attributes' => [
        'header' => TRUE,
        'rowspan' => 2,
        'colspan' => 2,
      ],
      '#plain_text' => $this
        ->t('Condition'),
    ],
    // Love to have 'Condition' column as colspan=3 and omit this header, but
    // theme renders 'operations' widget incorrectly when only one link is
    // present.
    [
      '#wrapper_attributes' => [
        'header' => TRUE,
        'rowspan' => 2,
        'width' => 100,
      ],
      '#plain_text' => $this
        ->t('Links'),
    ],
    [
      '#wrapper_attributes' => [
        'header' => TRUE,
        'rowspan' => 2,
        'colspan' => 2,
      ],
      '#plain_text' => $this
        ->t('Scope'),
    ],
    [
      '#wrapper_attributes' => [
        'header' => TRUE,
        'rowspan' => 1,
        'colspan' => 4,
      ],
      '#plain_text' => $this
        ->t('Operations'),
    ],
  ];
  $operations = [
    'create' => $this
      ->t('Create'),
    'view' => $this
      ->t('View'),
    'update' => $this
      ->t('Update'),
    'delete' => $this
      ->t('Delete'),
  ];
  foreach ($operations as $operation) {
    $form['table']['operations'][] = [
      '#wrapper_attributes' => [
        'header' => TRUE,
        'class' => [
          'checkbox',
        ],
      ],
      '#plain_text' => $operation,
    ];
  }
  $i = 0;
  $this->rules = $this->eventTypeRuleStorage
    ->loadByProperties([
    'entity_type' => $event_type
      ->getEventEntityTypeId(),
    'bundle' => $event_type
      ->getEventBundle(),
    'trigger' => $trigger,
  ]);
  foreach ($this->rules as $rule_id => $rule) {
    $i++;
    $scope_all = FALSE;
    $supports_create = 0;
    $condition_context = [];

    // Conditions.
    $k = 0;
    $row = [];
    $row['rule'] = [
      '#wrapper_attributes' => [
        'header' => FALSE,
      ],
      '#plain_text' => $this
        ->t('@row.', [
        '@row' => $i,
      ]),
    ];
    foreach ($rule
      ->getConditions() as $id => $condition) {
      $k++;
      $row[] = [
        '#wrapper_attributes' => [
          'header' => TRUE,
        ],
        '#markup' => $this
          ->t('@condition.', [
          '@condition' => $k,
        ]),
      ];
      $condition = $this->conditionManager
        ->createInstance($condition['id'], $condition);
      $condition_context += array_keys($condition
        ->getContextDefinitions());
      $scope_all = !in_array('registration', $condition_context) || in_array('event', $condition_context);
      if ($condition instanceof RNGConditionInterface) {
        $supports_create++;
      }
      $row[] = [
        '#markup' => $condition
          ->summary(),
      ];
      $row[] = [
        '#type' => 'operations',
        '#links' => [
          'edit' => [
            'title' => t('Edit'),
            'url' => Url::fromRoute('rng.rng_event_type_rule.component.edit', [
              'rng_event_type_rule' => $rule
                ->id(),
              'component_type' => 'condition',
              'component_id' => $id,
            ]),
            'query' => $this->redirectDestination
              ->getAsArray(),
          ],
        ],
      ];

      // Scope: warn user actions apply to all registrations.
      if ($scope_all) {
        $row[] = [
          '#wrapper_attributes' => [
            'colspan' => 2,
          ],
          '#plain_text' => $this
            ->t('Single registration'),
        ];
      }
      else {
        $row[] = [
          '#plain_text' => $this
            ->t('All registrations.'),
        ];
        $row[] = [
          '#wrapper_attributes' => [
            'width' => '20%',
          ],
          '#markup' => $this
            ->t('<strong>Warning:</strong> selecting view, update, or delete grants access to any registration on this event.'),
        ];
      }
      if ($k == 1) {
        $row = array_merge($row, $this
          ->actionRows($rule, $operations, $scope_all, $supports_create));
      }
      $form['table'][] = $row;
      $row = [];
    }
  }
  $form['settings'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Settings'),
  ];
  $form['settings']['custom_rules'] = [
    '#title' => $this
      ->t('Allow default rules customization'),
    '#description' => $this
      ->t('Allow event managers to customize event default rules. Changing this setting does not affect existing rules.'),
    '#type' => 'checkbox',
    '#default_value' => $event_type
      ->getAllowCustomRules(),
  ];
  return $form;
}