You are here

public function Pbf::fieldSettingsForm in Permissions by field 8

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

array The form definition for the field settings.

Overrides EntityReferenceItem::fieldSettingsForm

File

src/Plugin/Field/FieldType/Pbf.php, line 135

Class

Pbf
Plugin implementation of the 'pbf' field type.

Namespace

Drupal\pbf\Plugin\Field\FieldType

Code

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

  /** @var \Drupal\field\FieldConfigInterface $field */
  $field = $form_state
    ->getFormObject()
    ->getEntity();
  $entity_type = $field
    ->getTargetEntityTypeId();
  $bundle = $field
    ->getTargetBundle();
  $target_entity_type_id = $field
    ->getSetting('target_type');
  $cardinality = $field
    ->getFieldStorageDefinition()
    ->getCardinality();
  $form['synchronization'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Synchronization'),
    '#description' => '',
    '#open' => TRUE,
    '#tree' => TRUE,
    '#process' => [
      [
        get_class($this),
        'formProcessMergeParent',
      ],
    ],
    '#weight' => 10,
  ];
  $form['synchronization']['synchronized_by'] = [
    '#type' => 'value',
    '#value' => $field
      ->getSetting('synchronized_by') ? $field
      ->getSetting('synchronized_by') : '',
  ];
  if ($cardinality === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {

    // Only fields attached to user entity type can be synchronized by.
    if ($field
      ->getSetting('synchronized_by') && empty($field
      ->getSetting('synchronized_from_target'))) {
      $form['synchronization']['info'] = [
        '#markup' => $this
          ->t('Field synchronized by @field_id', [
          '@field_id' => $field
            ->getSetting('synchronized_by'),
        ]),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      ];
    }
    elseif ($field
      ->getSetting('synchronized_by') && $field
      ->getSetting('synchronized_from_target')) {
      $form['synchronization']['info'] = [
        '#markup' => $this
          ->t('Field synchronized by @field_id and allowed to synchronize it', [
          '@field_id' => $field
            ->getSetting('synchronized_by'),
        ]),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      ];
    }
    elseif (empty($field
      ->getSetting('synchronized_by')) && $entity_type == 'user') {
      $form['synchronization']['info'] = [
        '#markup' => $this
          ->t('Field not synchronized'),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      ];
    }
    elseif ($target_entity_type_id !== 'user') {
      $form['synchronization']['info'] = [
        '#markup' => $this
          ->t('Field which reference user entity type can be synchronized if eligible fields are found.'),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      ];
    }
  }
  else {
    $form['synchronization']['info'] = [
      '#markup' => $this
        ->t('Only field with an unlimited cardinality can be synchronized'),
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    ];
  }

  // No need to display theses options that are only relevant on Node entity
  // type.
  if ($entity_type !== 'node') {
    return $form;
  }

  // Priority parameter has been removed in Drupal 8, and will not be used
  // except by using Node access priority Module. See
  // https://www.drupal.org/project/napriority
  $form['priority'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Permissions priority'),
    '#open' => TRUE,
    '#tree' => TRUE,
    '#process' => [
      [
        get_class($this),
        'formProcessMergeParent',
      ],
    ],
    '#weight' => 1,
  ];
  $form['priority']['priority'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Priority'),
    '#description' => $this
      ->t('The priority to apply on permissions.
      If not sure about this, let the default priority to 0. If you have some
      issues with the permissions set, because you use multiple modules which
      handle node access, try to increase the priority applied to
      the permissions. Priority will be only used if the module Node access
      priority is installed. Permissions with the higher priority will be then
      used.'),
    '#default_value' => $field
      ->getSetting('priority') ? $field
      ->getSetting('priority') : 0,
  ];
  if ($target_entity_type_id == 'user') {
    $options = [
      'user' => $this
        ->t('Grant access directly to users referenced'),
      'ref_user' => $this
        ->t('Grant access directly to users referenced and
         grant access to users who reference those users from the field
         <em>@field_name</em> attached to user entity type', [
        '@field_name' => $field
          ->getName(),
      ]),
    ];
    $form['user_method'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Handle permissions for users'),
      '#open' => TRUE,
      '#tree' => TRUE,
      '#process' => [
        [
          get_class($this),
          'formProcessMergeParent',
        ],
      ],
      '#weight' => 2,
    ];
    $form['user_method']['user_method'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Choose method for grant access to users'),
      '#options' => $options,
      '#default_value' => $field
        ->getSetting('user_method') ? $field
        ->getSetting('user_method') : 'user',
    ];
    if ($cardinality === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {

      // We support for referenced user to synchronize them if they have a Pbf
      // field attached whose target bundle is the current bundle.
      // We get eligible fields to be synchronized.
      $fields = $this
        ->getPbfEligibleFields($target_entity_type_id, $bundle);
      $eligible_fields = [];
      if ($fields) {
        foreach ($fields as $field_id => $field_data) {
          $eligible_fields[$field_id] = $field_data['label'] . ' (' . $field_data['field_name'] . ')';
        }
      }
      $form['synchronization']['synchronized_with'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Select the field attached to users to synchronize'),
        '#options' => $eligible_fields,
        "#empty_option" => $this
          ->t('No synchronization'),
        '#default_value' => $field
          ->getSetting('synchronized_with') ? $field
          ->getSetting('synchronized_with') : [],
      ];
      $form['synchronization']['synchronized_from_target'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Allow targeted field from users to synchronize this field'),
        '#default_value' => $field
          ->getSetting('synchronized_from_target') ? $field
          ->getSetting('synchronized_from_target') : 0,
        '#return_value' => (int) 1,
        '#empty' => 0,
        '#states' => [
          'invisible' => [
            'select[data-drupal-selector="edit-settings-synchronized-with"]' => [
              'value' => '',
            ],
          ],
        ],
      ];
    }
  }
  return $form;
}