美文网首页
drupal8 create auto-compelete fo

drupal8 create auto-compelete fo

作者: AnnaJIAN | 来源:发表于2019-11-12 19:29 被阅读0次
/**
 * 添加搜索框可以编辑默认调用组
 *
 * Implements hook_form_FORM_ID_alter().
 *
 * @param array $form
 * @param FormStateInterface $form_state
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function portal_form_taxonomy_overview_terms_alter(array &$form, FormStateInterface $form_state)
{
  $current_path = \Drupal::service('path.current')->getPath();
  $is_permission_group = preg_match('/permission_group/', $current_path);

  if (!$is_permission_group) {
    return;
  }
  $config = \Drupal::service('config.factory')->getEditable('config.settings');

  if (!empty($config->get('default_cert_id')[0]['target_id'])) {
    $default_term =  \Drupal\taxonomy\Entity\Term::load($config->get('default_cert_id')[0]['target_id']);
  } else {
    // can't be '' and can't be NULL?;
    $default_term = '';
  }

  $form['field_default_cert_id'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'taxonomy_term',
    '#selection_settings' => [
      'target_bundles' => array('permission_group'),
    ],
    '#autocreate' => array(
      'target_bundles' => array('permission_group'),
      'bundle' => ('tags'),
    ),
    '#title' => ('默认调用组为'),
    '#tags' => TRUE,
    '#default_value' => $default_term,
    '#size' => '30'
  ];

  $form['actions']['submit']['#submit'] = [
    'callback' => '_form_taxonomy_overview_terms_form_submit',
  ];

}

/**
 * 更新默认调用组提交方法
 *
 * @param array $form
 * @param FormStateInterface $form_state
 */
function _form_taxonomy_overview_terms_form_submit(array $form, FormStateInterface $form_state) {

  $cert_id = $form_state->getValue('field_default_cert_id');
  $config = \Drupal::service('config.factory')->getEditable('config.settings');
  $config->set('default_cert_id', $cert_id)->save();
//  $tax = \Drupal::formBuilder()->getForm('taxonomy_overview_terms');
//  $tax::save($form, $form_state);
}

相关文章

网友评论

      本文标题:drupal8 create auto-compelete fo

      本文链接:https://www.haomeiwen.com/subject/opnqictx.html