You are here

public function CustomTextComponentType::addComponentForm in Flexiform 8

Get a form for adding a component of this type.

Parameters

array $form: The part of the form array that should contain the component plugin settings.

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

Return value

array A form array for setting the options of a component. Normally broken into settings and third_party_settings.

Overrides FormComponentTypeCreateableBase::addComponentForm

File

src/Plugin/FormComponentType/CustomTextComponentType.php, line 39

Class

CustomTextComponentType
Plugin for field widget component types.

Namespace

Drupal\flexiform\Plugin\FormComponentType

Code

public function addComponentForm(array $form, FormStateInterface $form_state) {
  $form['content'] = [
    '#title' => t('Content'),
    '#type' => 'text_format',
    '#required' => TRUE,
  ];
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $tree = [];
    $token_info = \Drupal::service('flexiform.token')
      ->getInfo();

    /** @var TreeBuilderInterface $tree_builder */
    $tree_builder = \Drupal::service('flexiform.token.tree_builder');
    foreach ($this
      ->getFormEntityManager($form, $form_state)
      ->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,
          ],
        ]);
      }
    }
    $form['tokens'] = [
      '#type' => 'token_tree_table',
      '#token_tree' => $tree,
      '#show_restricted' => TRUE,
      '#theme_wrappers' => [
        'form_element',
      ],
    ];
  }
  return $form;
}