2017-03-15 88 views
0

我有這個項目關於添加inline troc管理使用Drupal 8在Drupal 8中創建修改頁面

當我想修改一些數據時,我遇到了一些問題。

我想跳過默認頁面,因爲我無法到達此頁面中的列表選擇。

有沒有辦法逃離默認模式?

+0

歡迎來到SO。請提供更多關於您正面臨的問題的信息,例如堆棧跟蹤/日誌以及您迄今爲止所嘗試的內容。 –

+0

我創建了一個名爲'ajout-troc'的自定義模塊,它將創建一個名爲'troc'的新節點,並且作爲一個控制器,我創建了這個php文件,這非常有效(我不知道如何上傳它)。現在我想讓用戶編輯已經創建的節點,但我不知道該怎麼做。我有一個由drupal給出的編輯頁面,但我想定製它。 – Nawres

回答

0

這裏是我的插入代碼:

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"字段是一個列表選擇,因爲我有一些選項,我想user免費,並選擇他的最佳選擇。下面的鏈接是截圖。

Image link

+0

你爲什麼要用代碼分離問題?研究如何在stackoverflow中編寫更好的問題。 –

+0

對不起,這是我第一次在stackoverflow。 – Nawres

+0

我編輯了你的問題的代碼,所以你現在可以刪除這個答案。 – coderade