Warta Lab is one of the best websites for tutorials on PHP, Drupal, Drupal 8, and general programming topics. Visit us for expert solutions, in-depth guides, and valuable programming knowledge
What is Patch and How to create a patch in Drupal
drupal 8 how i create a custom form to programmatically created block and assign to it in variable in preprocess
Preprocess page assign a custom block to variables code
$customblock = \Drupal::service('plugin.manager.block')- >createInstance('custom_block_id', []);
$variables['home_meal_plan'] = $customblock->build();
*******************************************************************************
1 - Creating custom form
goto - custom_module\src\Form and create a customform.php
<?php
namespace Drupal\meal_plan\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\cms_pages\Controller\CommonFunc;
/**
* Provides meal plan form.
*
* @internal
*/
class HomeMealForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'home_meal_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
global $base_url;
$hindi = CommonFunc::isHindi();
if (\Drupal::currentUser()->isAnonymous()) {
$footer_content = CommonFunc::getCmsContent('field_meal_plan_footer_content');
$meal_plan_heading = CommonFunc::getCmsContent('field_meal_plan_heading');
$form['age'] = [
'#type' => 'textfield',
'#attributes' => array('class' => array('inptFld'), 'id' => array('home-meal-age') ,'autocomplete' => array('off')),
'#default_value' => '',
'#placeholder' => 'Eg. 7',
'#required' =>TRUE
];
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', 'food_type');
$query->sort('name');
$tids = $query->execute();
$options = [];
$options = [
'Vegan' => t('Vegan'),
'Veg' => t('Veg'),
'Eggetarian' => t('Eggetarian'),
'Pescatarian' => t('Pescatarian'),
'Nonveg' => t('Non-Veg'),
];
$options1 = [
'2-12' => t('2-12'),
'13-17' => t('13-17'),
'18-25' => t('18-25'),
'25-50' => t('25-50'),
];
$form['food_type'] = [
'#type' => 'radios',
'#options' => $options,
'#default_value' => 'Vegan',
'#required' => TRUE,
];
$form['age_range'] = [
'#type' => 'radios',
'#options' => $options1,
'#default_value' => '2-12',
'#required' => TRUE,
];
$form['footer_content'] =[
'#markup' => $footer_content,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t("Apply"),
'#attributes' => array('class' => array('primary-button hvr-ripple-out')),
'#validate' => ['::submitValidateMeal'],
];
$form['#theme'] = 'HomeMealForm';
$form['#meal_plan_heading'] = $meal_plan_heading;
$form['#attached']['library'][] = 'meal_plan/meal_style';
return $form;
}
}
/**
* {@inheritdoc}
*/
public function submitValidateMeal(array &$form, FormStateInterface $form_state) {
if ($form_state->getValue('age') == '') {
$form_state->setErrorByName('age', 'Please select date of birth');
}
elseif ($form_state->getValue('age') < 1) {
$form_state->setErrorByName('age', 'Minimum required age is 1 year.');
}
elseif ($form_state->getValue('age') > 12) {
$form_state->setErrorByName('age', 'The recommendations are for healthy children between 1 year to 12 years of age. Please enter appropriate age.');
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
global $base_url;
$age = $form_state->getValue('age');
$food_type = $form_state->getValue('food_type');
$age_range = $form_state->getValue('age_range');
$meal_data = ['age' => $age, 'food_preference' => $food_type];
$session = \Drupal::request()->getSession();
$session->set('meal_data', $meal_data);
$form_state->setRedirect('meal_plan.unreg_routine');
}
}
3- assign custom create Block to a variable in preprocess : in theme.theme file
/**
* Implements hook_preprocess().
*/
function theme_new_preprocess(array &$variables, $hook) {
$variables['base_path'] = base_path();
$variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
$current_path = \Drupal::service('path.current')->getPath();
$variables['current_path'] = $current_path;
$alised_path = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
$alised_path = ltrim($alised_path, '/');
$variables['alised_path'] = $alised_path;
$current_url = explode("/", $alised_path);
if($variables['is_front']) {
$customblock = \Drupal::service('plugin.manager.block')->createInstance('home_custom_meal_form_block', []);
$variables['home_meal_plan'] = $customblock->build();
}
}
Drupal 8 and Drupal 9 set and get COOKIE example with symphony HttpFoundation Component
$cookie = Cookie::create('foo')
->withValue('bar')
->withExpires(strtotime('Fri, 20-May-2011 15:25:52 GMT'))
->withDomain('.example.com')
->withSecure(true);n addition to the Cookie::create() method, you can create a Cookie object from a raw header value using fromString() method. You can also use the with*() methods to change
Drupal 8 cookies can be set using ResponseHeaderBag from the Symfony\Component\HttpFoundation\Response object.Set new cookie value use Symfony\Component\HttpFoundation\Cookie; $cookie = new use Cookie('cookie_name', TRUE); $response->headers->setCookie($cookie); return $response; Get cookies value $request->cookies->get('cookie_name');Pathauto Module functionalities and usage
The Pathauto module automatically generates URL/path aliases for various kinds of content (nodes, taxonomy terms, users) without requiring the user to manually specify the path alias. This allows you to have URL aliases like /category/my-node-title instead of /node/123. The aliases are based upon a "pattern" system that uses tokens which the administrator can change.
Write a program in PHP to reverse a number
A number can be written in reverse order. For example 12345 = 54321 <?php $ num = 23456; $ revnum = 0; while ($ num > 1) { $...
-
General Drupal Knowledge What are the key differences between Drupal 9 and Drupal 10? Click Here What are some of the major improvements i...
-
Drupal 11 has officially landed, and it’s more than just another major version. It marks a transformation in how we build, manage, and del...