在Drupal 8中创建修改页面

时间:2017-03-15 11:21:28

标签: drupal project drupal-8 modifier

我有一个关于使用inline troc添加Drupal 8管理的项目。

当我想修改某些数据时,我遇到了一些问题。

我想要转义默认页面,因为我无法访问此页面中的列表选择。

有没有办法逃避默认模式?

1 个答案:

答案 0 :(得分: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