Skip to content

Instantly share code, notes, and snippets.

@edutrul
Created June 26, 2017 02:45
Show Gist options
  • Select an option

  • Save edutrul/e13cfdbee9cc6afb45cd267f5b5b0cd1 to your computer and use it in GitHub Desktop.

Select an option

Save edutrul/e13cfdbee9cc6afb45cd267f5b5b0cd1 to your computer and use it in GitHub Desktop.
Create new form element(Dropdown) in drupal 8 ajax and set image from taxonomy term
<?php
namespace Drupal\ship_package\Form;
use Drupal\Core\Url;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Display the main dropdowns.
*
*/
class ShipPackageFlowForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ship_package_flow_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['ship_nation'] = [
'#title' => $this->t('Local or international ship'),
'#type' => 'select',
'#options' => [
'1' => t('National'),
'2' => t('International')
],
'#required' => TRUE,
];
$terms = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadTree('aerial_package_type', $parent = 0, $max_depth = 1, $load_entities = FALSE);
$packageTypeParents = [];
foreach ($terms as $term) {
$packageTypeParents[$term->tid] = $term->name;
}
$form['aerial_package_type_parent'] = [
'#title' => $this->t('Choose type of package'),
'#type' => 'select',
'#options' => $packageTypeParents,
'#ajax' => [
'callback' => '::getAerialPackageTypeOption',
],
'#suffix' => '<div class="aerial-package-type-parent-image"></div>',
'#required' => TRUE,
];
$form['aerial_package_type_child_wrapper'] = [
'#type' => 'container',
'#attributes' => ['id' => 'aerial-package-type-child-wrapper'],
];
$packageTypeParent = $form_state->getValue('aerial_package_type_parent');
if ($packageTypeParent) {
$terms = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadTree('aerial_package_type', $packageTypeParent);
$packageTypeChildren = [];
foreach ($terms as $term) {
$packageTypeChildren[$term->tid] = $term->name;
}
$form['aerial_package_type_child_wrapper']['aerial_package_type_child'] = [
'#title' => $this->t('Choose type of article'),
'#type' => 'select',
'#options' => $packageTypeChildren,
'#required' => TRUE,
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Continue'),
];
return $form;
}
/**
* Form submission.
*
* @param array $form
* Form structure.
* @param FormStateInterface $form_state
* Form state with all values.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$nation = $form_state->getValue('ship_nation');
$packageTypeParent = $form_state->getValue('aerial_package_type_parent');
$packageTypeChild = $form_state->getValue('aerial_package_type_child');
$url = Url::fromRoute('entity.taxonomy_term.canonical', [
'taxonomy_term' => $packageTypeParent
]);
$paths = explode('/', $url->toString());
end($paths);
$key = key($paths);
$packageTypeParentAlias = $paths[$key];
// @TODO: Improve: instead of fromUri use fromRoute and keep alias as well!.
//$url = Url::fromRoute('node.add', ['node_type' => 'arial']);
$url = Url::fromUri("internal:/carga/inicial/detalle?edit[field_aerial_package_type]=$packageTypeChild&edit[field_aerial_is_national]=$nation&paragraphs-tools-autoadd=node:field_aerial_detail:aerdet_package_$packageTypeParentAlias");
return $form_state->setRedirectUrl($url);
}
/**
* Implements callback for Ajax on aerial package type selection.
*
* @param array $form
* From render array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Current state of form.
*/
function getAerialPackageTypeOption(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$packageTypeParent = $form_state->getValue('aerial_package_type_parent');
$packageTypeParentTerm = Term::load($packageTypeParent);
$image = '';
if (!empty($packageTypeParentTerm->field_image->entity)) {
$image = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail', ///< @TODO: TBD image style with themer.
'#uri' => $packageTypeParentTerm->field_image->entity->getFileUri(),
];
}
$response->addCommand(new HtmlCommand('#aerial-package-type-child-wrapper', $form['aerial_package_type_child_wrapper']));
$response->addCommand(new HtmlCommand('.aerial-package-type-parent-image', $image));
$form_state->setRebuild(TRUE);
return $response;
}
}
@edutrul
Copy link
Author

edutrul commented Jun 26, 2017

screencapture-transporte-devstec-es-carga-inicial-1498442718767

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment