在Drupal8中创建修改页面
我有一个关于添加在Drupal8中创建修改页面,drupal,project,drupal-8,modifier,Drupal,Project,Drupal 8,Modifier,我有一个关于添加inlinetroc使用drupal8管理的项目 当我想修改一些数据时,我会遇到一些问题 我想退出默认页面,因为我无法访问此页面中的选择列表 有没有办法退出默认模式?这是我的插入代码: namespace Drupal\ajout_troc\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Component\Utility\UrlHelper; use
inlinetroc
使用drupal8
管理的项目
当我想修改一些数据时,我会遇到一些问题
我想退出默认页面,因为我无法访问此页面中的选择列表
有没有办法退出默认模式?这是我的插入代码:
namespace Drupal\ajout_troc\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;
class ContributeForm extends FormBase {
public function getFormId() {
return 'ajout_troc_contribute_form';
}
public function extractRubriqueOptions() {
$terms = \Drupal::service('entity_type.manager')
->getStorage("taxonomy_term")
->loadTree('rubrique', $parent = 0, $max_depth = NULL, $load_entities = FALSE);
$options = array();
foreach($terms as $term) {
$options[$term->tid] = $term->name;
}
return $options;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['rubrique'] = array(
'#type' => 'select',
'#title' => t('Rubrique'),
'#options' => $this->extractRubriqueOptions(),
'#required' => TRUE,
);
$form['titre'] = array(
'#type' => 'textfield',
'#title' => t('Titre du bien'),
'#required' => TRUE,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description détaillée'),
'#required' => TRUE,
);
$form['telephone'] = array(
'#type' => 'tel',
'#title' => t('Téléphone'),
'#required' => TRUE,
);
$form['valeur'] = array(
'#type' => 'number',
'#title' => t('Valeur à la vente'),
);
$form['annee'] = array(
'#type' => 'number',
'#title' => t('Année de fabrication'),
);
$form['photo'] = array(
'#type' => 'managed_file',
'#title' => t('Photo'),
'#upload_location' => 'public://images/',
'#required' => TRUE,
);
$form['label'] = array(
'#type' => 'label',
'#title' => t('Ce que vous souhaitez en échange'),
);
$form['echange'] = array(
'#type' => 'select',
'#title' => t('Sélectionnez les rubriques qui vous intéressent : '),
'#multiple' => true,
'#options' => $this->extractRubriqueOptions(),
'#required' => TRUE,
);
$form['descriptionechange'] = array(
'#type' => 'textarea',
'#title' => t('Décrivez ici ce que vous souhaitez en échange :'),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Enregistrer mon annonce'),
'#attributes' => array('class' => array('button', 'button--primary', 'js-form-submit', 'form-submit'))
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (strlen($form_state->getValue('telephone'))!=8) {
$form_state->setErrorByName('telephone', $this->t("Le numéro de téléphone doit contenir 8 chiffres."));
}
}
public function submitForm(array &$form, FormStateInterface $form_state)
{
$user = \Drupal::currentUser();
$fields = array(
'type' => 'troc',
'title' => 'Annonce',
'status' => 0,
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
'uid' => $user->id(),
'field_rubrique' => array(
'target_id' => $form_state->getValue('rubrique')
),
'field_titre' => array(
'value' => $form_state->getValue('titre')
),
'field_description' => array(
'value' => $form_state->getValue('description')
),
'field_telephone' => array(
'value' => $form_state->getValue('telephone')
),
'field_valeur' => array(
'value' => $form_state->getValue('valeur')
),
'field_annee' => array(
'value' => $form_state->getValue('annee')
),
'field_descriptionechange' => array(
'value' => $form_state->getValue('descriptionechange')
),
);
$echanges = $form_state->getValue('echange');
foreach ($echanges as $echange) {
$fields['field_echange'][] =array(
'target_id' => $echange
);
}
$photoFid = $form_state->getValue('photo');
if(!empty($photoFid[0])) {
$photoFid = $photoFid[0];
$photo = \Drupal\file\Entity\File::load($photoFid);
$photo->setPermanent();
$photo->save();
$fields['field_photo'] = array(
'target_id' => $photoFid,
);
}
$node = Node::create($fields);
$node->save();
}
}
有关更多信息,请参见界面截图。我希望“Rubrique”
字段是一个列表选择,因为我有一些选项,我希望用户可以自由选择他最好的选项。下面的链接是屏幕截图
欢迎来到SO。请提供更多关于您所面临问题的信息,如堆栈跟踪/日志以及您迄今为止所做的尝试。我创建了一个名为“ajout troc”的自定义模块,它创建了一个名为“troc”的新节点,作为一个控制器,我创建了这个php文件,它工作得非常好(我不知道如何上传它)。现在我想让用户编辑已经创建的节点,但我不知道怎么做。我有一个由drupal提供的编辑页面,但我想对其进行自定义。为什么要用代码来分隔问题?学习如何在stackoverflow中编写更好的问题。对不起,这是我第一次使用stackoverflow。我编辑了问题的代码,因此您可以现在删除此答案。