development

  • Drupal 8: Set entity field cardinality programatically inside a custom form

    This is how you can dynamically change a multi select entity field maximum number of selected items programmatically. //The sample codes below will get an entity form and render the entity form fields to a custom form. // Create the entity $entity = $this->orderItemStorage->create([ 'title' => 'Order Item', 'type' => 'bee', 'purchased_entity' => 'my_product_variation_id', 'quantity' => 1, 'unit_price' => '100', ]); // Add the entity to form_state $form_state->set('order_item', $entity); // Get the form display $form_display = $this->entityTypeManager->getStorage('entity_form_display')->load('commerce_order_item.bee.add_to_cart'); $form_state->set('order_item_form_display', $form_display); // Set the form parents to empty to avoid form rendering errors $form['#parents'] = []; // loop through the entity form fields foreach ($form_display->getComponents() as $name => $component) { $widget = $form_display->getRenderer($name); if (!$widget) { continue; } $items = $entity->get($name); if ($name === 'my_multiple_select_field') { // Get the field definition and field storage definition object and set the cardinality $definition = $items->getFieldDefinition(); $fieldStorageDefinition = $definition->getFieldStorageDefinition(); $fieldStorageDefinition->setCardinality(1); } $items->filterEmptyItems(); // Add the entity form field to your custom form $form[$name] = $widget->form($items, $form, $form_state); $form[$name]['#access'] = $items->access('edit'); }  
  • drupal 8

    Drupal 8: Redirect to page from Event Subscriber with destination query

    Create your Event Subscriber Create your module's services yml file and declare the event subscriber services: mymodule.autologin: class: Drupal\mymodule\EventSubscriber\AutoLogin tags: - {name: event_subscriber} Save this as mymodule.services.yml inside mymodule folder Create the actual class used from above code <?php namespace Drupal\mymodule\EventSubscriber; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class AutoLogin implements EventSubscriberInterface { public function redirectToLogin() { // PUT YOUR LOGIC HERE } /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = ['redirectToLogin']; return $events; } } Save this as AutoLogin.php inside mymodule/src/EventSubscriber folder   Redirect using URL::fromRoute with destination query $current_path = \Drupal::service('path.current')->getPath(); $url = \Drupal\Core\Url::fromRoute('simplesamlphp_auth.saml_login', [], ['query' => ['destination' => $current_path, 'absolute' => TRUE]]); $response = new RedirectResponse($url->toString(), 302); $event->setResponse($response); Put the above code in the AutoLogin::redirectLogin(). This code will get the current path and redirect to saml login page to auto login, then if the login is successful, it will redirect to the original path that we are trying to access.  
  • Change Virtual Environment Python Version

    Remove existing virtual environment Python files cd [EXISTING_ENV_PATH] rm .Python rm bin/pip{,2,2.7} rm bin/python{,2,2.7} rm -r include/python2.7 rm lib/python2.7/* rm -r lib/python2.7/distutils rm lib/python2.7/site-packages/easy_install.* rm -r lib/python2.7/site-packages/pip rm -r lib/python2.7/site-packages/pip-*.dist-info rm -r lib/python2.7/site-packages/setuptools rm -r lib/python2.7/site-packages/setuptools-*.dist-info Run the commands above to delete all python files (assuming that the old python version is python 2). Initialise Virtual Environment with new Python version virtualenv -p `which python3` . Run the command above inside the existing environment path.