Drupal StackExchange

How to create an Enquiry form with ability to submit a collection of enquiries

1 month 3 weeks ago

I am building a new site which has a large selection of different products/services on offer, the information for each is shown in it's own node.

If the user has interest in a number of these services then I would like to give them the ability to 'add' the item to their enquiry along with a message. When they have added all of the items they are interested in they can then submit the enquiry.

Overall, the process shares similarities with an e-commerce workflow

  1. View product
  2. Add product to enquiry form builder with comment (choose options and add to cart)
  3. Browse other products and repeat steps 1&2.
  4. Once happy, review enquiry (checkout)
  5. If all is ok, submit enquiry (process order)

Using Drupal 7, how would you suggest I tackle this?

I only compared the process to e-commerce as it helped me think things through, the site isn't actually going to be selling the products. Strictly a tool to submit a detailed enquiry.

leeboyce

How to use feeds tamper to set default value from importer node?

1 month 3 weeks ago

I've got a number of feeds that I want to be able to import regularly. I setup feeds and attached them to a content type, ex. "My Feeds" which imports into another content type, "Article".

I also need to make sure that when these feeds come in, that I can set the default values in the target node. The use case is that I will be importing from feeds in multiple languages and want them associated with an author so I have created a couple of new fields in my importer content type:

field_language field_author

I want to be able to set the language and author value for each feed when I create the importer and have those associated every time I update but its not clear if any of the existing plugins do this.

I've also started writing my own custom plugin to pull in these values from the parent importer but I can't figure out how to access or load the importer object through a feeds tamper plugin.

Any pointers or is there a better way to acccomplish this task?

schnippy

What's the coding standard for class constants?

1 month 3 weeks ago

For general constants, Coding standards / Naming Conventions says that constants should always be all-uppercase, with underscores to separate words and that module-defined constant names should also be prefixed by the module module machine name, all-uppercase, and an underscore character.

Object-oriented names don't follow the normal conventions of prefixing the name with the module or using underscores; instead, they opt for various forms of camel case.

Unfortunately, Object-oriented code doesn't mention class constants at all.

Which of the following is correct?

  • const FOO_CONSTANT_VALUE = 'bar';
  • const CONSTANT_VALUE = 'bar';
  • const constantValue = 'bar';
user7

CORS localhost ajax request

1 month 3 weeks ago

I have an HTML page (http://localhost:8888/test/test.html) in which I'm trying to pull data from a Drupal 7 website.

I'm using this AJAX code with HTML-Request. I have tried JSON and the website prevents me from pulling, even I have added the following code to pull the data.

<?php header('Access-Control-Allow-Origin: *'); ?>

This is the HTML page that requests AJAX from Drupal 7 website. Could you please check it for me?

<script src="http://code.jquery.com/jquery-latest.js"></script> <script> (function ($) { $(document).ready(function () { $.ajax({ // The link we are accessing. url: "http://horizon-websolutions.com", // The type of request. type: "get", // The type of data that is getting returned. dataType: "html", error: function () { alert("error"); // Load the content in to the page. $("#output").html("<p>Page Not Found!!</p>"); }, beforeSend: function () { alert("not yet"); }, complete: function () { alert("done"); }, success: function (strData) { alert("success"); // Load the content in to the page. $("#output").html(strData); } }); // Prevent default click. return(false); }); })(jQuery); </script> <div id="output"></div>

Please help me telling the website to accept my localhost request to pull data from the server. I have tested the same code on a pure PHP page and it is working fine.

Mohammad Moussa

How do you join a custom table to a node from a field with hook_views_data()

1 month 3 weeks ago

I have a custom table that contains (for simplicity) two fields:

  • id
  • client_id

On my node content type, I have an integer type field called: field_dashboard_id. All my nodes have the field_dashboard_id set to ids from the custom table. I have a content view that I would like to also display the client id from the custom table. (FYI I am able to create a view if I use Dashboard Entries instead of Content but I want to use Content and join to Dashboard Entries)

I tried the following code but when I try to add a relationship, I am not seeing my custom table in Views.

/** * Implements hook_views_data(). */ function mydashboard_views_data() { $data = []; $data['mydashboard_site']['table']['group'] = t('Dashboard'); $data['mydashboard_site']['table']['base'] = [ 'field' => 'id', 'title' => t('Dashboard entries'), 'help' => t('Contains a list of dashboard entries.'), ]; $data['mydashboard_site']['table']['join'] = array( 'node' => array( 'left_table' => 'node__field_dashboard_id', 'left_field' => 'entity_id', 'field' => 'id', ), ); $data['mydashboard_site']['id'] = [ 'title' => t('ID'), 'help' => t('Unique Site ID.'), 'field' => [ 'id' => 'numeric', ], 'filter' => [ 'id' => 'numeric', ], 'argument' => [ 'id' => 'numeric', ], 'sort' => [ 'id' => 'standard', ], 'relationship' => [ 'base' => 'node', // The name of the table to join with. 'base field' => 'field_dashboard_id', // The name of the field on the joined table. // 'field' => 'nid' -- see hook_views_data_alter(); not needed here. 'handler' => 'views_handler_relationship', 'label' => t('Default label for the relationship'), 'title' => t('Title shown when adding the relationship'), 'help' => t('More information on this relationship'), 'extra' => [ [ 'field' => 'field_dashboard_id', 'value' => 'value', 'operator' => '=', 'numeric' => true, ], ], ], ]; $data['mydashboard_site']['client_id'] = [ 'title' => t('Client ID'), 'help' => t('The client id'), 'field' => [ 'id' => 'standard', ], 'filter' => [ 'id' => 'string', ], 'argument' => [ 'id' => 'string', ], ]; return $data; }

Update

Also tried the following:

$data['mydashboard_site']['table']['join'] = [ 'node__field_dashboard_id' => [ 'left_field' => 'field_dashboard_id_value', 'field' => 'id', ], ]; 'relationship' => [ 'base' => 'node__field_dashboard_id', // The name of the table to join with. 'base field' => 'entity_id', // The name of the field on the joined table. // ID of relationship handler plugin to use. 'id' => 'standard', // Default label for relationship in the UI. 'label' => t('Example node'), ],
albertski

Getting values on twig node template

1 month 3 weeks ago

I have installed ECK module in my website. Using it I have created a "report eck" entity in which I have different bundles like section, notes, etc., each containing different fields. I have my content type "Report" in which I have referred "report eck" entity using entity reference field. Now, in my nice template, I need to get various values including the bundle type, in which I am facing lots of trouble. My bundle type is coming up as following when I am doing kint() on my reference field, but I am not able to get this bundle type and many other values, please guide.

Also, it seems to be a protected values, so I navigated to "Available Methods" tabs and found method 'getEntityKey()' which seems to be useful, but how can I use it in twig template??

Dev

How come my view block is not loading using ajax?

1 month 3 weeks ago

We have a view, myview, that has an exposed filter title (text input). The view has Exposed form in block:Yes and so we load it onto the page under admin/structure/block. The block has a machine name of exposedform_myview_title.

We want to remove it from display and only load the view block on click of an icon. How come my view block is not loading when using ajax?

View Setup:

  • view_name = 'myview`
  • when creating view, view page setting -> "create a page" was set to true
  • view machine name = mypage
  • view use ajax = Yes
  • view block (displayed using admin/structure/block) machine name exposedform_myview

Error message

https://somesite/path/views/ajax?view_name=myview&view_block_id=mypage 404 (not found)

myjs.js

This will load the view itself and not the exposedform_myview_title block. We want the exposedform_myview_title to be displayed.

(function($) { 'use strict'; Drupal.behaviors.iconClick = { attach: function(context, setting) { $('#someid', context).once().each(function () { $(this).on('click', function(clickEvent) { // Use Ajax to load form. $.ajax({ url: drupalSettings.path.baseUrl + 'views/ajax', type: 'GET', // type: 'POST' not succesfull dataType: 'json', data: 'view_name=myview&view_display_id=mypage', //not successfull --> data: 'view_name=myview&view_block_id=exposedform_myview', success: function(response) { var output = response[1].data; $('.some-element').html(output); }, error: function(data) { alert('error'); console.log(data); } }); }); }); } }; })(jQuery);

page.html.twig

... <div id="someid">my icon goes here</div> <div class="some-element"> view block exposed form should go here </div> ...

we also tried to use Drupal.ajax, but that did not load the View into the required <div class="some-element">

(function($) { 'use strict'; Drupal.behaviors.iconClick = { attach: function(context, setting) { $('#someid', context).once().each(function () { $(this).on('click', function(clickEvent) { // Use Ajax to load form. var ajaxPath = '/views/ajax'; var view_info = { view_name: 'myview', view_display_id: 'page' }; var ajax_settings = { submit: view_info, url: ajaxPath, event: 'click' }; Drupal.ajax(ajax_settings); }); }); } }; })(jQuery);
usernameabc

How to safely output user input HTML in a twig theme [duplicate]

1 month 3 weeks ago

Very basic question:

I have a twig template were I want to output the HTML content of a CkEditor managed field within a node.

<div class="wrap-user-html"> {{ node.field_myfield.0.value }} </div>

This does not work, as it prints the html code with html entities. I'm getting e. g.

<div class="wrap-user-html"> &lt;p&gt;foo&lt;/p&gt; </div>

I could use |raw and it would work, but I read this is unsafe. Also when using raw, text format filters from the ckeditor settings seem to not get applied.

How do I do this right?

c1u31355

Drupal Planet