Drupal StackExchange

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

1 day 17 hours 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

1 day 17 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

1 day 18 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?

1 day 19 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

1 day 20 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

1 day 21 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

1 day 21 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

How do I enable a REST resource for a custom content type?

1 day 23 hours ago

I've created a bunch of custom content types like "Car Makes" and "Car Models", but I can't seem to figure out how to enable a REST resource for them. In my REST settings page, I see the default content, content type, user, user registration enabled and a lot of disabled types below it. This list doesn't contain any of my custom created types.

If I go to the permissions page, under "RESTful Web Services", I only see "Administer REST resource configuration" and "Access POST on User registration resource". How can I enable "Car Makes" as a REST resource and directly GET/POST with it?

I saw this screenshot, but I don't get anything like this in Drupal 8.4:

In this screenshot, Todo is a custom resource, which requires writing a plugin. Do I have to write a plugin module just to expose each of my custom content types in REST?

I can work directly with the content endpoints, i.e. POST /node/123, but I would like to POST /carmakes, for example.

aalaap

Drupal Planet

Balint Pekker: Simplifying Drupal Documentation

2 days ago
Are you new to the world of Drupal, eager to dive into its powerful capabilities, but encountering obstacles in the form of outdated tutorials and confusing documentation? You're not alone. Beginning your Drupal journey can feel like navigating uncharted territory, leaving many feeling frustrated before they even begin.