Drupal 8 form and view in block - forms

I am very new Drupal, In a block I would like to have a form with select box and submit button. Each options in the link is link of content page. That is when the user select an option and click the submit button it will redirect to other node page.
For the above requirement client used Web Form for Drupal 7 and I would like to clone the requirement for Drupal 8. I tried EForm and I am able to create a form but I can not able to show the form in Block Layout and View.
I am not sure the module Eform is suitable for my requirement.
Can you guys please help me what modules do I need to install for the above requirement in Drupal 8.

Okay, I don't know if there are any modules out there which do exactly what you want to, but you can build your own custom solution. In my eyes there are two main possibilities:
create a custom block type with a HTML body field, put your HTML in there and you're done. Advantage: easy to do, Disadvantage: hardcoded
clean way: create a new node type and/or a new category, which you will use for your country nodes. Then you'll create a block programmatically and query for all nodes of that certain country type or for nodes with the "country page"-category, whatever you use to organize those nodes. Then you just create a form out of that data and render it.
Advantage: dynamic, the select list will update itself whenever you add or delete new nodes of that type / category. Disadvantage: takes more effort initially
I personally would recommend using option 2. Option 1 is better for really simple and "stupid" requirements like showing some hardcoded text/image on several places on your site, or if it's something temporary like some campaign teaser, which will be over in 1 week and you'll throw it away after that.
EDIT:
Entity Query: https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A%3AentityQuery/8
How to build Forms in Drupal 8:
https://www.drupal.org/node/2117411

For display only your block in your templates with preprocess the best way is:
$block = \Drupal\block\Entity\Block::load('my_block_id');
$variables['My_region'] = \Drupal::entityManager()
->getViewBuilder('block')
->view($block);
And in your page.html.twig or node.html.twig or xxx.html.twig use your variable My_region like this :
{% if page.My_region %}
{{ page.My_region }}
{% endif %}
For details check:
https://drupal.stackexchange.com/questions/171686/how-can-i-programmatically-display-a-block

Related

How to cross-validate contact form 7 form fields?

I want to make two sperate check box groupe. Lets say
Front end courses
-Bootstrap
-React Js
-Angular js
Back end courses
-C#
-Node
-PHP
on submitting form I want to make sure at least one course is selected by the user. Lets say a student selects ReactJs.
I have tried below code but it makes one option mandatory for all checkbox groups
[Checkbox* checkbox-11 ",Bootstrap" "React Js" "Angular JS"]
[Checkbox* checkbox-12 ",C#" "Node" "PHP"]
How can I make at least one option selected mandatory from any of the checkbox groups
field validation takes place post form submission. To solve this problem you need to make neither field mandatory, and then do a custom validation on the server side. CF7 plugin allows you to add custom validation to individual field but does not allow you to validate a field wrt to the value of other fields in the form.
To do this you need an extension such as the Smart Grid-layout plugin which has a validation hook that gives you access to the entire form's submitted data and therefore cross-validate your fields,
add_filter('cf7sg_validate_submission', 'cross_validate_submission',10,3);
function cross_validate_submission($validation, $submission, $form_key){
if(empty($submission['fe-courses']) && empty($submission['be-courses'])){
$validation['fe-courses']='You need to select at least 1 course!';
}
return $validation;
}

Make twig plugins by dynamically horizontal reusing via "use" (not "include")

Need: A dynamic template loaded from a DB
I need to present a JSON object via "template plugins" that will come from a database (defined outside the application itself). The plugin will be applied only if a plugin exists.
For this example, let's assume I have this object of type "Reservation" that contains a sub-object of type "Flight":
{
"id": "ABC-XYZ",
"reservationDate": "2020-09-23",
"state": "paid",
"flight":
{
"origin": "BCN",
"destination": "MAD",
"airline": "VY"
}
}
Rules
The controller will call a page template passing multiple objects.
The page template will "use" or "include" (see later more info) an object.html.twig and will display it.
The object.html.twig will do this:
If there's not any known plugin able to handle this type of object, the template will display a <pre> with the object converted to YAML. This will be mainly act as a "default" plugin or "fallback" plugin.
If there's a known plugin able to handle the object, the object will be sent to the plugin (which is not anything else than another twig template).
In this case, the plugin should be able to separate "parts" of "interpretable" results to make them "nice" and leave the rest into an object that will be, in turn, displayed again with the original "default" plugin.
If there are parts of the object that are in turn interpretable, they will be in turn passed to other plugins.
Example "desired" outputs
Sample 1. No plugin available
Sample 2. Plugin 'reservation' available. Interprets the state in green. Also removes the redundant data of the ID
Sample 3. Same than 2 but also plugin 'flight' available, able to process the flight block. Formats the texts and makes a lookup of the airline full-name
Sample 4. Plugin 'flight' available, able to process the flight, that in turn knows that the origin and the destination are "airports" and passes them into the corresponding nested 'airport' plugin, because they are "reusable" objects not only in the "flight" plugin but also in many other places in the application, so they are defined appart
What I have already explored
I know that doing it via 'include' it could work. But let's take a look at the differences between 'use' and 'include':
The major difference between include and use in twig is that:
When you include a template, it's direct HTML 'inserted there' where you can use the {{ }} operator for printing and {% %} for control flow (set variables, ifs, fors, etc.). The renderer will process it straight forward. But defining a new block via {% block myNiceBlock %} is forbidden.
When you 'use' a template, it's pre-loaded, and blocks are permitted. There's no rendering of the included block. Then, from the caller, you use a {{ block( 'whatever' ) }} to tell the renderer to go and render that specific block.
The 'include' is more rudimentary. The use allows horizontal reusing and allows itself to auto-organize itself with other sub-blocks called by the parent block, all in one single file.
For example, in the airports example, if there are N images, in a include you should put the wrapper HTML directly in the file, do a loop and inside the loop write the inner HTML.
Instead in the use approach you'd do an airport block which in turn loops over the images and just calls the block airportImage which is defined in another block in the same file, thus facilitating clean-coding.
Requirement
The application should not be re-deployed when new plugins are created. They must be loaded from a DB or any other dynamic system, as the plugins will be written by "users of the application" as they need it. No deploy allowed.
The plugins should be written in terms of a "block-able" twig template, so need to be 'use'-able.
Question
Discovering "which" plugin to call is not a problem. Assume that whoever (the controller, the twig itself, whoever really) can discover for this example that a "reservation" plugin exists somewhere. How can I use it from the page? When the reservation is rendering it "asks if a flight plugin" is available. If not, all to the YAML. If it is, how can then dynamically tell the reservation to use the flight?
In short: How do I force a template to dynamically use (not include) templates that, in turn, comes from the database (not from fixed files)?
NOTE: If this info is useful: I'm using Symfony 5 with webpack.
Thanks!

Laravel 4 - get data from multiselect form

I'm using Laravel 4, and I have two tables related 'Many to many': 'Actividad' and 'Material'. Every Actividad can have one or more Materials, and every Material can belong to one or more Actividad.
So, I have made a form to create a new Actividad where you can save one or more Materials. I've done that with a multiselect input. Like that:
{{ Form::label('material_id', 'Material necesario:') }}
<p>{{ Form::select('material_id', $material_id, Input::old('material_id'), array('multiple')) }}</p>
I don't know if I'm doing correctly but, before saving anything, my first problem is that I'm only obtaining one result. I suppose I should get every option 'checked' in the form... The relevant part of my Controller is:
$material = Input::get('material_id');
return var_dump($material);
I should obtain a list of options selected, but the result of that in my browser is:
string(1) "1"
This is the first time I'm working with tables related in this way, and probably I'm doing something wrong (in the form, in my controller, in my Models,...)
Thank you very much for your help!!
Just change your code to this
{{ Form::select('material_id[]', $material_id, Input::old('material_id'), array('multiple')) }}
I hope this helps.
if you are using custom response handlers on the client side such in the case of submitting info with AJAX, all you need to do is to simple add "[]" to the name of the select control.
e.g.
<select name="material_id[]" multiple>
This is the same case as with regular PHP. The other methods are required if you want Laravel to handle the form elements for you when rendering a new response/view. ( page reload ). This reload doesn't happen with REST requests.

Can i create Customizable Menu's in umbraco

im trying to create customizable menu in umbraco. i.e. user should be able to add /remove / edit any menu item in menu. (User will not be a developer)
but i dont know how to do that..i've heard about Macros but dont know much about them so cant use it.
I think this has been done before also..
Thanks in advance
Generally, your menu will reflect your node structure within umbraco. This is the easiest way to allow your clients control of the site's navigation. If there are nodes that you would rather not have in the menu, that you could use the umbracoNaviHide property on the document type.
Try out some of the starter kits that are available. They will come with macros that build the navigation based on your nodes and will give you a good idea of how they work. You can even start by using a starterkit and then just modify it as you like. That's what I would recommend as you start out with umbraco. Umbraco has about 4 or so built in starterkits and Our Umbraco has several more that other users have contributed.
To use the default navigation template provided with Umbraco:
If you log into the Umbraco backoffice and head over to the Developer section, should should see Scripting Files. Right-click Scripting Files and choose Create. Choose a filename, like Nav and and from the "Choose a template" menu, select Site Map, then click Create. You should end up with the following razor code:
#*
SITEMAP
=================================
This snippet generates a complete sitemap of all pages that are published and visible (it'll filter out any
pages with a property named "umbracoNaviHide" that's set to 'true'). It's also a great example on how to make
helper methods in Razor and how to pass values to your '.Where' filters.
How to Customize for re-use (only applies to Macros, not if you insert this snippet directly in a template):
- If you add a Macro Parameter with the alias of "MaxLevelForSitemap" which specifies how deep in the hierarchy to traverse
How it works:
- The first line (var maxLevelForSitemap) assigns default values if none is specified via Macro Parameters
- Next is a helper method 'traverse' which uses recursion to keep making new lists for each level in the sitemap
- Inside the the 'traverse' method there's an example of using a 'Dictionary' to pass the 'maxLevelForSitemap' to
the .Where filter
- Finally the 'traverse' method is called taking the very top node of the website by calling AncesterOrSelf()
NOTE: It is safe to remove this comment (anything between # * * #), the code that generates the list is only the below!
*#
#inherits umbraco.MacroEngines.DynamicNodeContext
#helper traverse(dynamic node){
var maxLevelForSitemap = String.IsNullOrEmpty(Parameter.MaxLevelForSitemap) ? 4 : int.Parse(Parameter.MaxLevelForSitemap);
var values = new Dictionary<string,object>();
values.Add("maxLevelForSitemap", maxLevelForSitemap) ;
var items = node.Children.Where("Visible").Where("Level <= maxLevelForSitemap", values);
if (items.Count() > 0) {
<ul>
#foreach (var item in items) {
<li>
#item.Name
#traverse(item)
</li>
}
</ul>
}
}
<div class="sitemap">
#traverse(#Model.AncestorOrSelf())
</div>
This will produce a ul/li menu of the structure of your site. You plug this into your template by inserting the macro.
Take a look at the default Top Navigation template for XSLT or Razor. That should give you an idea of where to start and how navigation generally works in Umbraco. I second Douglas' answer that the Navigation usually mirrors the content structure in the Content section.
If you really want a setup where you add items to the nav independent of the Content tree structure, then use a Multinode Tree Picker on your Home Page and have that be the navigation in your Top Nav macro.

How to make multistep forms with drupal using various node types

I'm creating a very basic classifieds website. In this website i'll have various content-types; such as :
Car (which has the cck fields : year, kilometers, color
House (which has the cck fields : number of floors, garden (yes/no)
So each 'element' is a content-type.
I'm listing all the content-types in a view that I display to the user aand then clicking on a link goes to 'create content type of type (clicked type)'.
It's working pretty well; but i can't get rid of the 'create new car' at the top of the create page (which reflects the 'drupalish' behaviour).
I'd like to have it in a more conveniant way such as a three step form like :
Choose category
Choose your options
Register to post your new classified
I've seen the ctools; which provide 'almost' the multistep behaviour; however i can't imagine having all my dozen content-types being 'hardcoded' in a single module.
I wonder if anyone has achieved this kind of setup or if there's a kind of module that can do the trick. I'd like to keep a content type for each type of classified (the webmaster is now used to the interface).
Any help, starting points would be appreciated.
For the first step we had to solve a simular problem. To do so we created what was basicly an override of the /node/add page (the one that lists all the content types), which you've done. To change the title the simplest is to create a yourtheme_preprocess_page() function that changes the title when the url is /node/add or node/*/edit
However: I would strongly suggest switching to a system that uses 1 content type for all listings. We created a very simular site, and after working with different content types it because clear that having 1 content type with fields that were displayed conditionally was a much more sane solution. Using categories for the different product types, and then using the Conditional Fields module to hide and show the correct fields worked much better.
http://drupal.org/project/conditional_fields
Here is and example snippet for setting the title in a page preprocess function:
Setting the title on the node/add page:
if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == '') {
$vars['title'] = 'Choose an Industry';
$vars['head_title'] = $vars['title'] . " | " . variable_get('site_name', "Industry Trader");
}