You are here

public function CustomTextComponent::settingsForm in Flexiform 8

Get the settings form.

Parameters

array $form: The form array.

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

Overrides FormComponentBase::settingsForm

File

src/Plugin/FormComponentType/CustomTextComponent.php, line 96

Class

CustomTextComponent
Component class for field widgets.

Namespace

Drupal\flexiform\Plugin\FormComponentType

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $sform = [];
  $sform['admin_label'] = [
    '#title' => t('Admin Label'),
    '#description' => t('Only shown on administrative pages'),
    '#type' => 'textfield',
    '#default_value' => $this->options['admin_label'],
    '#required' => TRUE,
  ];
  $sform['content'] = [
    '#title' => t('Content'),
    '#type' => 'text_format',
    '#default_value' => $this->options['content'],
    '#format' => !empty($this->options['format']) ? $this->options['format'] : NULL,
    '#required' => TRUE,
  ];
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $tree = [];
    $token_info = $this->token
      ->getInfo();

    /** @var TreeBuilderInterface $tree_builder */
    $tree_builder = \Drupal::service('flexiform.token.tree_builder');
    foreach ($this
      ->getFormEntityManager()
      ->getContextDefinitions() as $namespace => $context_definition) {
      $entity_type_id = $context_definition
        ->getDataType();
      list(, $entity_type_id) = explode(':', $entity_type_id, 2);
      if ($namespace == '') {
        $namespace = 'base_entity';
      }
      $entity_type = \Drupal::entityTypeManager()
        ->getDefinition($entity_type_id);
      $token_type = $entity_type
        ->get('token_type') ?: (!empty($token_info['types'][$entity_type_id]) ? $entity_type_id : FALSE);
      if ($token_type) {
        $tree[$namespace] = $token_info['types'][$token_type];
        $tree[$namespace]['tokens'] = $tree_builder
          ->buildTree($token_type, [
          'parents' => [
            $namespace,
          ],
        ]);
      }
    }
    $sform['tokens'] = [
      '#type' => 'token_tree_table',
      '#token_tree' => $tree,
      '#show_restricted' => TRUE,
      '#theme_wrappers' => [
        'form_element',
      ],
    ];
  }
  return $sform;
}