Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

1

Simple solution of your problem: public function build() { $message = \Drupal\contact\Entity\Message::create(['contact_form' => 'feedback']); return \Drupal::service('entity.form_builder')->getForm($message); }


1

I found out a solution which I ended with using {{ element.[machine name] }} instead of {{ children }} on my custom template which is cloned by form.html.twig. The result of the code look similar below: <div class="row"> <div class="col-md-6"> {{ element.name }} {{ element.mail }} {{ element....


0

Someone else had the same problem - this is the reason: "In the video the form shows the form with no warnings, but when I try it shows the following warning: Notice: Trying to get property 'nid' of non-object in Drupal\rsvplist\Form\RSVPForm->buildForm() This is caused by getting $node = \Drupal::routeMatch()->getParameter('node'); in a page with any node....


0

Line line 29: $nid = $node->nid->value; Try this solution: $node->id(); Very useful thing "Drupal 8 Entity API cheat sheet": https://www.metaltoad.com/blog/drupal-8-entity-api-cheat-sheet


0

The error message isn't helping you out much here. In an ideal world it would be more like this: The value for element with key '0' is not an array, but also not NULL. That would be more in-keeping with the logic that triggered the error in Element::children(), which is expecting each element whose key isn't prefixed with # to itself be a render array. ...


0

I think this error relates to using an ordinal array, rather than an associative array, for the $results variable. I think you might be able to fix it by replacing this loop: foreach ($data ['items'] as $item ) { $results[] = $item['name']; } With this one: foreach ($data['items'] as $key => $item ) { $results[$key] = $item['name']; } Hope it ...


0

You can use this approach. /** * Implements hook_field_widget_form_alter(). */ function insights_analyst_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) { if($element['#field_name'] == 'field_figure'){ $element['#process'][] = 'removeFile'; } } function removeFile(&$element , \Drupal\Core\Form\...


1

It should be possible with some code like this: function MY_MODULE_form_entity_browser_MY_FORM_form_alter(&$form, FormStateInterface $form_state, $form_id) { $form['#pre_render'][] = 'MY_MODULE_pre_render'; } function MY_MODULE_pre_render($element) { kint($element); //Find the tab you want to remove. if (...) { unset($element['...']); }...


0

Ok if all else fails here is a sneaky trick by setting a new form state object. use Drupal\Core\Form\FormState; ... $form_state = new FormState(); $form_state->set('name', $name); $form_state->set('user', $user); $render_array = \Drupal::formBuilder()->buildForm('\Drupal\mod\Form\MyForm', $form_state); ... // Example render. $response = new ...


0

I used below Solution. Its worked for me. Click Here


1

you'll probably need to post some sort of code to help us understand whats going on. What version of Drupal are you on? What HTTP status code is coming back from Drupal? if its a status 200, Are there any errors, messages etc being returned? but at a high level, Drupal protects against CSRF and the 3rd party site submitting a form on the Drupal site would ...


1

So you added this MY_EMAIL_FIELD to the form with form_alter. Add the following as well: $form['actions']['submit']['#submit'][] = 'MYMODULE_node_form_send_email'; Add another function to your file: function MYMODULE_node_form_send_email($form, FormStateInterface $form_state) { $email = $form_state->getValue('MY_EMAIL_FIELD'); // send email here } ...


1

A field added in the form_alter hook is not created on the entity. Therefore you cannot retrieve the value in either hook_entity_presave or hook_entity_insert. What you want however you can do by creating an additional submit handler for the form. function _MODULE_NAME_custom_submit_handler($form, FormStateInterface $form_state) { $email = $form_state-&...


1

You can add an extra validation step to those forms that checks and changes the value accordingly like so. function polaris_drupal_taxonomy_form_alter(&$form, FormStateInterface &$form_state, $form_id) { switch ($form_id) { case 'taxonomy_term_category_form': case 'taxonomy_term_article_type_form': case 'taxonomy_term_main_purpose_form'...


2

You can use BaseFieldDefinition::setCardinality() for that, e.g.: $fields['fichier'] = BaseFieldDefinition::create('file') ->setLabel(t('Fichier attaché')) ->setSetting('file_directory', 'messagerie/fichier') // dossier d'upload ->setSetting('max_filesize', '10MB') // taille max du fichier ->setSetting('file_extensions', 'pdf doc txt xls ...


1

Set the cardinality to unlimited: ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)


0

For those who end up in this situation, the reason for this to happen is an error somewhere in your PHP code that is not caught by the PHP interpreter or Drupal itself and the server disconnects to your browser immediately before sending any information (including for example the web server certificate). It is not a network-related error as suggested above ...


0

From 8.2 you can use hook_ENTITY_TYPE_prepare_form() E.G. function hook_ENTITY_TYPE_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Form\FormStateInterface $form_state) { if ($operation == 'edit') { $entity->label->value = 'Altered label'; } } So assuming your second form is a node, you could do: function ...


0

You must specify your validate function. See here: http://www.codeexpertz.com/blog/drupal/drupal-8-custom-form-validate-and-submit-hookformalter-example


2

I created amap.services.yml services: listenrolments: class: Drupal\amap\ListEnrolments arguments: [] and ListEnrolments.php <?php namespace Drupal\amap; class ListEnrolments { public function __construct() { } public function list() { some code... return $rows; } } and in my two forms, inserted $rows = \Drupal::...


1

As they share a significant part of code; I put this code in a public function _listEnrolments located in the first form. If it's functionality that two classes share equally, neither class should own it; it should be in a service, and the service should be injected into each form class. Otherwise you'll have to do something pretty awkward (and not good ...


0

This is often achieved using Views with a Contextual filter that pulls in the current node ID and uses it as the "parent" to find the "children" of that Entity Reference field. Here is a similar question/answer with all the steps how to set it up: List "ER Child" info on "ER Parent" nodes https://drupal.stackexchange.com/a/280876/52224 Using Views is ...


1

Use entityQuery to search for referenced entities. Let’s illustrate a couple of examples of querying for node entities using this object. A very simple query that returns the published nodes: function ecc_general_node_update(Node $node) { // Find the project for current client that are actives $query = \Drupal::entityQuery('node') ->...


2

This is tricky in D8, as the ajax code does not offer any hooks or callbacks after the function has run. The solution is to override the ajax callback function that the managed_file element uses, adding an additional custom command that is triggered when the file upload is complete. First, we create a custom AJAX command, that will be used to trigger the ...


2

Should be: public function customCallback(array &$form, FormStateInterface $form_state) { $list = $form_state->get('list'); $form['fieldset']['list']['#description'] = $list; return $form['fieldset']; }


0

In Drupal 8, you can try this if you want to add certain attributes to the views filter (in my case, it is a select dropdown) Add this to your mymodule.theme file function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) { $form['field_month_target_id']['#attributes']['class'][] = 'GAEventTrack'; $form['...


1

I don't have time to provide an exact answer, but something along these lines. // Programmatically execute view $view = views_get_view('sel_selistes'); // view machine name $view->set_display("entity_reference_1"); // view display name // $view->set_arguments($arg) // looks like you don't have arguments. $view->execute(); $view_results = []; // ...


2

The D8 uid property reference isn't something that can be cast into a string as you could in D7. In D7, user objects use a generic PHP object class (stdClass) with a uid property containing a string. In D8, user objects use a Drupal\user\Entity\User class. This class does't have a direct uid string property. In D8 the uid property magically retrieves the ...


1

I think you will need a custom module to do that An outline of what your module should be doing Create a simple module using the docs or following a simple module tutorial or by studying the examples module Extend it by creating a form using drupal 8 form API Validate on submit that the input fields are filled in properly On formSubmit you should ...


1

I finally got it working, I was kind of lost. The key was to correctly submit the image at blockSubmit function and effectively load the file at the build function This is the final code that is working for me <?php namespace Drupal\onyx_experiencia\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Block\BlockPluginInterface; use Drupal\...


Top 50 recent answers are included