You are here

public function OrganizationTypeForm::form in CRM Core 8.2

Same name and namespace in other branches
  1. 8.3 modules/crm_core_contact/src/Form/OrganizationTypeForm.php \Drupal\crm_core_contact\Form\OrganizationTypeForm::form()
  2. 8 modules/crm_core_contact/src/Form/OrganizationTypeForm.php \Drupal\crm_core_contact\Form\OrganizationTypeForm::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

modules/crm_core_contact/src/Form/OrganizationTypeForm.php, line 21

Class

OrganizationTypeForm
Class OrganizationTypeForm.

Namespace

Drupal\crm_core_contact\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /* @var \Drupal\crm_core_contact\Entity\OrganizationType $type */
  $type = $this->entity;
  $form['label'] = [
    '#title' => $this
      ->t('Name'),
    '#type' => 'textfield',
    '#default_value' => $type
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this organization type. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
    '#required' => TRUE,
    '#size' => 32,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $type
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => [
      'exists' => 'Drupal\\crm_core_contact\\Entity\\OrganizationType::load',
      'source' => [
        'label',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this organization type. It must only contain lowercase letters, numbers, and underscores.'),
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $type
      ->getDescription(),
    '#description' => $this
      ->t('Describe this organization type.'),
  ];

  // Primary fields section.
  $form['primary_fields_container'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Primary Fields'),
    '#description' => $this
      ->t('Primary fields are used to tell other modules what fields to use for common communications tasks such as sending an email, addressing an envelope, etc. Use the fields below to indicate the primary fields for this organization type.'),
  ];

  // @todo Move primary fields array to some hook. This Would allow extend this
  // list to other modules. This hook should return array('key'=>t('Name')).
  $primary_fields = [
    'email',
    'address',
    'phone',
  ];
  $options = [];
  $type_id = $type
    ->id();
  if (isset($type_id)) {

    /* @var \Drupal\Core\Field\FieldDefinitionInterface[] $instances */
    $instances = \Drupal::service('entity_field.manager')
      ->getFieldDefinitions('crm_core_organization', $type_id);
    $instances = isset($instances[$type_id]) ? $instances[$type_id] : [];
    foreach ($instances as $instance) {
      $options[$instance
        ->getName()] = $instance
        ->getLabel();
    }
  }
  foreach ($primary_fields as $primary_field) {
    $form['primary_fields_container'][$primary_field] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Primary @field field', [
        '@field' => $primary_field,
      ]),
      '#default_value' => empty($type
        ->getPrimaryFields()[$primary_field]) ? '' : $type
        ->getPrimaryFields()[$primary_field],
      '#empty_value' => '',
      '#empty_option' => $this
        ->t('--Please Select--'),
      '#options' => $options,
    ];
  }
  return $this
    ->protectBundleIdElement($form);
}