Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • create a custome block in drupal 8

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 529
    Comment on it

    Hi All. Today i m going to share a custome block through which we can use content title as a option value to redirect at a particular url.

    For this we create a custome block and  do some database coding in our controller.

    Step 1

    create a folder which you want to assign your module..In my case it carmodulesearch.

    create info file inside this directory like carmodelsearch.info.yml

    name: Car Model Search
    description: 'Car Model Search module'
    type: module
    core: 8.x
    

    Step 2 Create a controller under src/Controller direcoty.

    <?php
    
    /**
     * @file
     * Contains \Drupal\carmodelsearch\Form\carmodelsearchForm.
     */
    
    namespace Drupal\carmodelsearch\Form;
    
    use Drupal\Core\Form\ConfigFormBase;
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Core\Database\Database;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    
    class CarModelForm extends ConfigFormBase
    {
    
        /**
         * {@inheritdoc}.
         */
        public function getFormId()
        {
            return 'carmodelsearch_form';
        }
    
        /**
         * {@inheritdoc}.
         */
        public function buildForm(array $form, FormStateInterface $form_state)
        {
    
            $form = parent::buildForm($form, $form_state);
    
            $query = \Drupal::database()->select('node_field_data', 'nfd');
            $query->fields('nfd', ['nid', 'title']);
            $query->addField('nfl', 'field_link_uri');
            $query->addField('nfl', 'field_link_title');
            //$query->addField('ufd', 'field_link_url');
            $query->join('node__field_link', 'nfl', 'nfl.entity_id = nfd.nid');
            $query->condition('nfd.type', 'car_model');
            $cars = $query->execute()->fetchAllAssoc('nid');
    
            $options = [];
            foreach ($cars as $car) {
                $options[$car->field_link_uri] = $car->field_link_title;
            }
            
            $form['search'] = array(
                '#type' => 'select',
                '#title' => $this->t('Find a map update'),
                '#options' => $options
            );
            $form['actions']['#type'] = 'actions';
                $form['actions']['submit'] = array(
                  '#type' => 'submit',
                  '#value' => $this->t('Go'),
                  '#button_type' => 'primary',
                );
    
            return $form;
        }
    
        /**
         * {@inheritdoc}
         */
        public function validateForm(array &$form, FormStateInterface $form_state)
        {
    
        }
    
        /**
         * {@inheritdoc}
         */
        public function submitForm(array &$form, FormStateInterface $form_state)
        {
            $response = new RedirectResponse($form_state->getValue('search'));
            $response->send();
        }
    
        /**
         * {@inheritdoc}
         */
        protected function getEditableConfigNames()
        {
            return [
                'carmodelsearch.settings',
            ];
        }
    
    }
    

    then create block directory and put this code.

    <?php
    
    namespace Drupal\carmodelsearch\Plugin\Block;
    
    use Drupal\Core\Access\AccessResult;
    use Drupal\Core\Block\BlockBase;
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\Core\Session\AccountInterface;
    
    /**
     * Provides a 'carmodelsearch' block.
     *
     * @Block(
     *   id = "carmodelsearch_block",
     *   admin_label = @Translation("carmodelsearch block"),
     * )
     */
    
    class CarModelBlock extends BlockBase {
    
      /**
       * {@inheritdoc}
       */
      public function build() {
    
         $form = \Drupal::formBuilder()->getForm('Drupal\carmodelsearch\Form\CarModelForm');
        return $form;
    
      }
    
    }
    

    This code create a custome block with name carmodelsearch.

     

    If you get any problem you can contact with me.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: