Drupal StackExchange

Allow user to fill out node form, then force registration

5 days ago

I'm making a classified website.

Have a button that say "Make a ad" I would like the Anonymous user to be able to fill in the text for the ad.

But the Anonymous users after filling in advert. has to sign up for the ad to be posted

How can do this with drupal please?

many thanks for your help

Best Regards Charles

user3350619

Invalidating render cache items for anonymous?

5 days 1 hour ago

We have a certain content type in Drupal 8 that has a small grid of data on it that is populated from an API call when the node view is being built.

In that call, I am checking for cache and setting cache instead of talk to the API every request:

if ($cache = $this->cache->get($cache_key)) { return $cache->data; } // $content = request... $this->cache->set($cache_key, $content, REQUEST_TIME + 3600, ['mymodule']);

Where $cache_key is just mymodule:remote_id:subscription_id. My default cache bin is handled via Memcache.

The Drupal page cache time is set to 30 minutes, and Varnish sits in front of it for anonymous traffic.

I am noticing that even though my custom cache data is set for an hour timer, and the Drupal cache is set for 30 minutes, users are able to see data from 3 days ago but I am not sure how. As soon as I go view the site (authenticated) and refresh, the data updates.

I tried backtracking this but nothing jumped out as missing, perhaps other than some cache tags I might need to add. Even still, the render object is what is being shown from cache, which the data is a part of before it is cached, not the cached data item itself.

System cron is running every hour. Shouldn't it purge invalidated cache as part of system_cron?

Edit: I switched my local from Memcache back to the default cache (MySQL) and noticed render cache items all have an expiration of -1.

At what point are entity render cache items invalidated? I feel like that might be my issue, but not sure how to address it.

Kevin

Display success message after submitting form

5 days 1 hour ago

I am try to display an success message after submitting my form and redirect to next page. The code I am using is the following.

public function submitForm(array &$form, FormStateInterface $form_state) { drupal_set_message(t('Form Submitted Successfully'), 'status', TRUE); }

It doesn't work for me.

Is there any alternate way to do it? What code should I use to display a success message?

Karthiga

Is it better to create a taxonomy term using the 'create' method of the base Entity class or the entity storage manager?

5 days 1 hour ago

I've seen multiple ways to programmatically create entities.

In this case I'm creating a taxonomy term. One is using this method

$term = \Drupal\taxonomy\Entity\Term::create([ 'name' => $name, 'vid' => $vocabulary, ])->save();

Where I'm using the base entity class's public static create function. Another way to perform the action is using an entity storage manager. Like so:

$term = [ 'name' => $name, 'vid' => $vocabulary, ]; $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->create($term);

I looked up the 'create' function of the base Entity class and it seems to just be implementing what appears in the second example I provided. Is there a better practice between the two? Or does it not matter?

UPDATE: Also is there a difference between what the two return? Unless I'm doing things wrong I noticed that the top method returned an int which I'm assuming is the term id.

Amy

Loading current revision node

5 days 2 hours ago

How do I construct an entity condition so that it loads current revision of all nodes? This is my attempt.

$query = new EntityFieldQuery(); $result = $query->entityCondition('entity_type', 'node') ->entityCondition('bundle', 'xxxxx') ->fieldCondition('xxx_module','tid', $tid,'=') ->age(FIELD_LOAD_CURRENT) ->execute();

Instead of loading the current revision, it loads the published revision.

KMC

Have Views based Taxonomy Term page use term description for title

5 days 3 hours ago

I am using a view to replace the standard Taxonomy Term page (turned on the standard one in Drupal 8.x). I want the Title of the page to be changed from the Name of the term to the Description of the term everywhere it appears, in the Title attribute in the Header but more importantly in the Title that is rendered on the page. I have tried all sorts of tricks in a custom page--taxonomy.html.twig file to no avail.

Scott Steinkerchner

How to get exposed filters for entity reference to render?

5 days 4 hours ago

I am using a entity reference view and I am able to filter via taxonomy terms directly, but when I set it to exposed and let the user decide, it does not display on the form. How can I get the expose functionality to actually work?

It works on other things like pages and blocks, but not entity reference.

I have Views Entity Reference Filter and Better Exposed Filters already installed, but that didn't seem to fix the issue.

Shawn

Cannot format date for date field in a Views_Database_Connector view

5 days 5 hours ago

I am using the Views Database Connector module in drupal 8 to display the data from an external mssql database. I am running into issues with the date field. When I am asked to configure this field in the view, I am asked to choose a format for the date. However, when I go to view the page, I get this error (and stacktrace):

InvalidArgumentException: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp() (line 172 of core/lib/Drupal/Component/Datetime/DateTimePlus.php).

Drupal\Core\Datetime\DateFormatter->format('2009-03-02', 'html_year', '', 'UTC', 'en') (Line: 323)

format_date('2009-03-02', 'html_year', '', NULL) (Line: 177)

Drupal\views\Plugin\views\field\Date->render(Object) (Line: 1144) Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object) (Line: 224) template_preprocess_views_view_field(Array, 'views_view_field', Array) (Line: 287) Drupal\Core\Theme\ThemeManager->render('views_view_field', Array) (Line: 435) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)

I have not been able to figure out how to get the date to be converted to a timestamp so it can be formatted. I have tried a preprocessor but the template_preprocess_view_view_field always occurs first and the exception occurs before it gets to my preprocess function. Is there something particular about the date data type in mssql? Any suggestions how to get the date to not generate the exception?

Chris Howell

Programmatically update field collection data in custom entity

5 days 5 hours ago

I am in desperate need of help.

I have a custom entity called Question which has a bundle called data. Within the data bundle, I have a collection field called options with two subfields called correct_option & misc_option.

-Question -Data -options -correct_options -misc_options

I am desperately trying to programmatically write data into those 2 option fields but the following code is not working:

$field_collection_item_value = 1596; // Get field collection item value. $field_collection_item = entity_load('field_collection_item', array($field_collection_item_value)); // Load that field collection item. $field_collection_item->save(); // Save field-collection item.

The 1569 is the ID of an existing question entity.

Whenever the code is executed I get the following error:

Fatal error: Call to a member function save() on array

I would appreciate any help. I'm at a loss and not sure how to get this working.

sisko

Taxonomy Term showing strange text

5 days 6 hours ago

Having taxonamy term issues - as glossary terms showing with text below the text saying "There is currently no content classified with this term."

does anyone know how to remove or hide the statement mentioned?

Drupal 7

saraNPN