Trying get started with custom module development on Drupal 8. Don't have previous Drupal dev background. Been reading and watching some materials in the past 2 or so weeks.
I decided to dare doing a little more "complex" other than "Hello World", but I got stuck on how would I actually go about this.
This is what I have so far:
I created a content type for "people". It's very simple, all it has is the person's name and birthdate (date only), so you can use the form to input a few people.
I created a view that displays the list of people, their names and their birthdate. Of course everything so far is using the core admin thing.
Now, I wanted to create a custom module that calculates the person's age (based on the date stored in the DB). It takes today's date, subtracts the person's birthdate, and calculates their age.
Then, with some magic, it returns the age to the render/page output and now this calculated value (the age) will be displayed on the view as well along with the person's name and birthdate.
I assume I would need to "hook into" a place somewhere, where the list of people is returned from the database. Then I loop through the data, get the birthdate, calculate the age, stick the result (age) back into the data, and then the page/view will display this - somehow.
Of course I am already stuck on how would I go about doing this? Where would I need to hook into? With what API? And then of course, I created a view already, but the view doesn't have the "age" field - since that is calculated on the fly. So where and how would I display it?
So many questions...
If anyone would know some tutorial that is similar to this, I'd appreciate it. It's kinda tough getting started with the "custom" side of Drupal.
Thanks for some tips!
without custom programming
You can actually do this without custom coding. For this you can I would use either of the following modules field_token_value or computed_field.
They both create a field without giving the user a textbox to input any value.
The value of such fields are calculated from predefined rules/custom coding/tokens defined per field.
from your custom theme
The easiest way is to do this on the theming layer and not store this value in the database. You have to use hook_preprocess_node in your THEMENAME.theme
function THEMENAME_preprocess_node(&$variables){
//install debug and kint to be able to use
//kint($variables);
$node = $variables["node"];
if($node->getType() == "CONTENTTYPE" && $node->hasField('field_date')){
//get date value something like "2018-09-07T21:35:30"
$date = $node->field_date->value;
//your logic and age calculations
// ...
$age = 35;
//set variable to use in node.html.twig based templates
$variables["person_age"] = $age;
}
}
In order to get more information on what is available install debug module and enable kint (which is part of debug) to be able to see variables using kint function
Than copy node.html.twig from your custom theme (or parent theme or classy core theme) to your THEMENAME/templates folder and rename it node--CONTENTTYPE.html.twig.
In there you can include the variable you just created in proprocess.
person age: {{ person_age }}
Make sure you clear the cache for all those changes to be seen by Drupal.
FOR MORE INFO
To find out what twig templates to override see here
More on twig and getting available variables
Getting more info on which twig template is used can also be obtained by enabling debug mode on previous link.
I would suggest you pick up a book on drupal development to understand all those concepts in a more concrete way.
Somewhere after my store update to version 1.6.1 (not sure if this is related), I lost the ability to change the currency on the store.
What I already did:
Changed theme to default;
Disabled overrides;
Cleared caches and cookies;
Uninstalled/Installed Currency Block.
No results, so far. How far did I get:
On setCurrency() call, ajax call is returning 1... so I believe it's working that far.
The page is reloading, but the currency remains the same.
What I believe it's happening: Cookie is not being set!
In this function on ChangeCurrencyController.php, I believe something is not working (maybe the line before ajaxDie('1') where cookie is set):
public function initContent()
{
$currency = new Currency((int)Tools::getValue('id_currency'));
if (Validate::isLoadedObject($currency) && !$currency->deleted)
{
$this->context->cookie->id_currency = (int)$currency->id;
$this->ajaxDie('1');
}
$this->ajaxDie('0');
}
There's a friend's computer where the cookies weren't cleared since the last updates, where this is working. I believe he has a cookie that is updated on currency change. If I delete his cookies I guess it will stop working. Or is this nonsense?
Can someone give me a hand here?
Can it be something else that has nothing to do with the cookies?
Thanks,
I had the same problem, it is not a bug, when you change the currency prestashop change it back based on your browser language. There is an option to turn off in localization > localization > settings > define default country based on browser language > off
I have written an extension for Virtuemart 1.1.9 which retrieves products from the virtuemart database. This primarily uses the '#__vm_product" table.
Now that I have started to write a Virtuemart 2.x compatible version I have some questions.
I have noticed that my product table containing the product names is in "#__virtuemart_producs_en_gb"
I am assuming that virtuemart has detected my language during install, and created these tables with the "en_gb" suffix automatically.
My questions are:
What is the best way to detect what the suffix is likely to be on these product tables, as I am assuming that the suffix could be anything in theory, "en_us" for example, or "fr"?
Is there a config value I can search for which contains this language setting?
Any ideas about the best way to tackle this issue?
Regards
James
Actually, this may be a way of doing it...
$query="SELECT lang_code FROM #__languages";
$db->setQuery($query);
$lang_code = $db->loadResult();
$lang_code = strtolower(strtr($lang_code,'-','_'));
//this will fetch "EN-GB" from the database, and change it to "en_gb"
any thoughts on its reliability?
I'm making a crawler and I only want to use U.S. domains. For example, I would want:
http://thenorthface.com/
but I would not want:
http://uk.thenorthface.com
or
http://se.thenorthface.com/
Does anyone know of a way to do this or a perl module that does this? I know it could be done with regex, but I'm trying to avoid having to get together a list of all foreign domain beginnings... Thanks a lot!
You cannot reliably determine what a "US" domain is from the URL. It's not even clear that the term "US domain" has any meaning.
For example, many US state abbreviations are also ISO-3166 country codes. What will you do with ar.xyz.com. Is that Arkansas or Argentina? What about ma.pdq.com... Massachussetts or Morocco (Maroc in French)?
You may be able to link second-level domains to a country (for a headquarters at least) but hostnames and third-level domains will be impossible to classify.
Anyone know of a good webservice or api that I can use to get the sunrise/sunset times in bulk? Every thing I have found so far only gives a day at a time or has a limitation on what dates can be used.
http://sunrise-sunset.org/api
It's free to use. Just needs a credit link back to the website.
It's not a web service, but this SO question has links to algorithms, so you can create a table or your own web service with all the dates you need.
I recently found this JavaScript library that performs calculation based on date and lat/lon coordinates. It seems to be very precise.
Link: https://github.com/mourner/suncalc
It is also available as NodeJs package through npm.
NASA has the calculation in nicely laid out JS. View the source of this page:
http://www.esrl.noaa.gov/gmd/grad/solcalc/
NOTE: I'm not a lawyer but I believe the US Government cannot copyright anything, hold patents, etc. so one should be able to copy as use as one needs.
This is a nice and free sunrise and sunset times API: http://sunrise-sunset.org/api
PHP has built in functions to calculate sunrise/sunset:
http://php.net/manual/en/function.date-sunrise.php
http://php.net/manual/en/function.date-sunset.php
Weather underground has this:
wunderground astronomy feature
Keys are free and they have a pretty generous policy for volume. Been using their weather actuals and forecast json forecast for about year, couldn't be happier.
EarthTools comes up first on google here at webservices sunrise sunset
The NASA one is cool, but the US Naval Observatory has one (below) that could actually pass for an API. If you want to make it useful beyond its intended purpose:
inspect the http headers to find out what parameters are being sent
parse the hell out of the response
It was a fun exercise. You should be able to send a location (long/lat or City/State) along with a year to obtain a list of sunrise and sunset times for an entire year (and other data as well).
http://aa.usno.navy.mil/data/docs/RS_OneYear.php
Go to this website > https://www.esrl.noaa.gov/gmd/grad/solcalc/
1. Set your location
Select your location. You can zoom in to your location and move the pin manually.
2. Verify your Location and Date
Scroll down to the 'Location' under the map and verify your location.
The date is automatically taken, if not set it manually.
3. Get the details
Click the button 'Create Sunrise/Sunset Tables for the Year'.
4. Output in a Table
New window opens and all the details are displayed as table.
Thanks to #noctonura for the link.
Let's go to http://www.earthtools.org/webservices.htm
It's free web service that provides Timezone and Sun times from Latitude/Longitude location.
There is a way to calculate the sunrise/sunset without the need of an API. Its mostly based on location. Sorry I dont have much more info.
Home page
Query url
Url syntax:
http://sunpath.azurewebsites.net/api/values/LAT/LON/ALT/TIMEZONE
You must replace LAT, LON, ALT and TIMEZONE with your data
.
JSON result:
{"zenith":55.365660255995422,"azimuth180":25.434155784212443,"azimuth":205.43415578421244,"incidence":55.365660255995422,"suntransit":12.423540739046871,"sunrise":6.9577562375305817,"sunset":17.899687249200021,"time":"2016-02-23T13:49:31.3816733Z"}
Or you can access online version of SPA.c algorithm:
Online query for SPA algorithm
Output is not in JSON format, but you can specify multiple times and get multiple results:
Date,Time,Top. azimuth angle (westward from S),Topocentric sun declination,Topocentric sun right ascension,Top. elevation angle (uncorrected)
2/23/2016,0:00:00,168.224314,-10.130760,335.659091,-57.643946
2/23/2016,1:00:00,-164.161551,-10.115560,335.699290,-57.227919
2/23/2016,2:00:00,-140.171655,-10.100376,335.739465,-51.963801
2/23/2016,3:00:00,-122.026618,-10.085209,335.779585,-43.529014
2/23/2016,4:00:00,-108.202194,-10.070054,335.819621,-33.425695
2/23/2016,5:00:00,-96.857772,-10.054907,335.859549,-22.544581
2/23/2016,6:00:00,-86.694509,-10.039764,335.899355,-11.406096
2/23/2016,7:00:00,-76.801678,-10.024618,335.939030,-0.376624
2/23/2016,8:00:00,-66.440121,-10.009464,335.978575,10.203398
2/23/2016,9:00:00,-54.907983,-9.994297,336.017999,19.930206