【问题标题】:Adding elements to a field widget in Drupal 8在 Drupal 8 中向字段小部件添加元素
【发布时间】:2017-08-01 00:40:05
【问题描述】:

我需要完成的是添加指向字段小部件的节点的链接(该节点包含正确编译字段的格式化指令)。

到目前为止,我已经能够使用第三方设置在字段配置表单中添加节点引用字段:

function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === 'field_config_edit_form' && $form_state->getFormObject()->getEntity()->get('entity_type') == 'myentity') {

    $field = $form_state->getFormObject()->getEntity();
    $help_page_id = $field->getThirdPartySetting('mymodule', 'help_page_id');

    $form['help_page_id'] = array(
      '#type' => 'entity_autocomplete',
      '#title' => t('Help page'),
      '#target_type' => 'node',
      '#selection_handler' => 'default',
      '#selection_settings' => array(
        'target_bundles' => array('help_page'),
      ),
      '#default_value' => $help_page_id ? Node::load($help_page_id) : NULL,
      '#weight' => 100,
    );

    $form['#entity_builders'][] = 'mymodule_form_field_config_edit_form_builder';
  }
}

现在我在显示表单和更改字段小部件以显示链接时无法检索此信息。 使用 hook_field_widget_form_alter,我无法从我拥有的参数中获取自定义配置值:

function mymodule_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {

}

我想我必须加载字段配置实体,但我不知道如何。

【问题讨论】:

  • 我想看到你的代码会更容易找到你的问题。从你说的情况来看,很难知道是什么问题,可能是你的代码有问题,也可能是缓存问题。
  • 我用代码编辑了原始消息。谢谢。

标签: drupal drupal-8


【解决方案1】:

我没有测试它,但是这样的东西应该可以工作:

function mymodule_field_widget_form_alter(&$element, \Drupal\Core\Form\FormStateInterface $form_state, $context) {
  // Add a suffix to widget form elements for all fields of type entity_reference.
  $field_definition = $context['items']->getFieldDefinition();
  if ($field_definition->getType() == 'entity_reference') {
    // TODO: Render the node link to $link
    $element['#suffix'] = $link;
  }
}

【讨论】:

  • 我已编辑代码以检查是否设置了第三方设置,但您的提示是正确的。要查找的上下文是 $context['items']->getFieldDefinition()。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 2017-05-16
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多