For only the second time since Drupal 8 went beta, no coding changes for the Optimizely module were needed for a new release of D8.
It's pretty amazing that a small module like this one, which uses only a very small subset of the API's, can reveal quite a few changes just by attempting to run through its normal functionality, its error checking, and automated tests.
Back in July of last year, I wrote about function cache_clear_all() being removed along with its wildcard for subpaths. At the time, I simply replaced that function with a clearing of the entire cache where any wildcard is used in the D7 version.
However, that cache invalidation must be re-written for Drupal 8 so that it works in a targeted manner and does not potentially cause such a big hit to performance.
Now that D8 has become much more stable, it's time I stopped procrastinating and do it right.
May 10, 2015
May 4, 2015
Notes on object-oriented JavaScript for o-o developers (part 2)
This post continues with more notes on distinctive features of object-oriented JavaScript as described in the book The Principles of Object-Oriented JavaScript.
The post is a follow-up to the earlier Part 1.
** Regardless of how a function is defined and what its declared signature, it can be called with any number of parameters without triggering a runtime error.
Almost always, you do want to pass and to use the parameters as declared and as named. If for no other reason, do this for the sake of making the code better self-documenting.
Otherwise, within the context of each function there is a special array called arguments[] that contains the actual params for the call. You can use arguments[] to get a count of params and to access their values.
** There is no function overloading.
If a function is defined more than once, it is the most recent definition that is used. The others are effectively overwritten.
** There are special methods on functions.
Since functions are objects, it's possible that those objects have methods. There are at least three: call(), apply(), and bind().
They are a bit too complicated to write about for these concise notes, so I won't describe them any further. I just wanted to mention that they exist and to reinforce the notion that functions are objects to which methods can be applied.
** Objects can be made non-modifiable in different degrees.
(1) You can prevent properties from being added to an object by calling Object.preventExtensions() on it. In other respects, the object is still modifiable.
var obj = new Object;
Object.preventExtensions(obj);
obj.greeting = 'hello, world'; // Not allowed.
(2) You can prevent properties from being added or removed from an object by calling Object.seal() on it. The values of the properties can be changed.
This makes the object similar to those created in a strongly-typed language with classes.
var obj = new Object;
obj.name = 'Turtle';
Object.seal(obj);
obj.greeting = 'hello, world'; // Not allowed.
delete obj.name; // Not allowed.
obj.name = 'Tortoise'; // Okay.
(3) You can prevent properties from being added or removed, and also prevent the values of properties from being changed by calling Object.freeze() on it. In general, this is sometimes called an immutable object.
var obj = new Object;
obj.name = 'Turtle';
Object.freeze(obj);
obj.greeting = 'hello, world'; // Not allowed.
delete obj.name; // Not allowed.
obj.name = 'Tortoise'; // Not allowed.
(Continued at Part 3.)
The post is a follow-up to the earlier Part 1.
** Regardless of how a function is defined and what its declared signature, it can be called with any number of parameters without triggering a runtime error.
Almost always, you do want to pass and to use the parameters as declared and as named. If for no other reason, do this for the sake of making the code better self-documenting.
Otherwise, within the context of each function there is a special array called arguments[] that contains the actual params for the call. You can use arguments[] to get a count of params and to access their values.
** There is no function overloading.
If a function is defined more than once, it is the most recent definition that is used. The others are effectively overwritten.
** There are special methods on functions.
Since functions are objects, it's possible that those objects have methods. There are at least three: call(), apply(), and bind().
They are a bit too complicated to write about for these concise notes, so I won't describe them any further. I just wanted to mention that they exist and to reinforce the notion that functions are objects to which methods can be applied.
** Objects can be made non-modifiable in different degrees.
(1) You can prevent properties from being added to an object by calling Object.preventExtensions() on it. In other respects, the object is still modifiable.
var obj = new Object;
Object.preventExtensions(obj);
obj.greeting = 'hello, world'; // Not allowed.
(2) You can prevent properties from being added or removed from an object by calling Object.seal() on it. The values of the properties can be changed.
This makes the object similar to those created in a strongly-typed language with classes.
var obj = new Object;
obj.name = 'Turtle';
Object.seal(obj);
obj.greeting = 'hello, world'; // Not allowed.
delete obj.name; // Not allowed.
obj.name = 'Tortoise'; // Okay.
(3) You can prevent properties from being added or removed, and also prevent the values of properties from being changed by calling Object.freeze() on it. In general, this is sometimes called an immutable object.
var obj = new Object;
obj.name = 'Turtle';
Object.freeze(obj);
obj.greeting = 'hello, world'; // Not allowed.
delete obj.name; // Not allowed.
obj.name = 'Tortoise'; // Not allowed.
(Continued at Part 3.)
Apr 29, 2015
Notes on object-oriented JavaScript for o-o developers (part 1)
I've started reading The Principles of Object-Oriented JavaScript by Nicholas C. Zakas.
So far, it's been excellent, one of the best technical books I've read. It's clear, concise, and doesn't have any fluff, a solid tutorial with fewer than one hundred pages.
Coming from having programmed a lot in the strongly-typed language C++ a couple of lifetimes ago, I find JavaScript very fluid and sometimes very different in certain aspects.
It has some commonalities with PHP, which is what I'm mostly working in these days and still learning.
The purpose of this post and its follow-up is to highlight those JavaScript features described in this book that might be of particular interest to someone with a background similar to mine.
** Javascript does not have classes nor class definitions in the usual sense.
It does have objects, which are instances of reference types. The built-in reference types include Object, Array, Date, Function, RegExp, and Error.
** An object is a dynamic collection of properties and their values.
Properties can be added or removed from an object at any time. In other words, by default, an object is not constrained by whatever type was used to instantiate it.
For example, create a Date object, then add an arbitrary property to it. Note that assigning a value to a property that didn't exist before implicitly adds that property to the object.
var date = new Date;
date.blessing = 'May you live in interesting times';
Then delete the property. The date object is now back to its initial state.
delete date.blessing;
** The primitive types, which include string, number, and boolean, sometimes can be treated as objects even though they are not objects.
Each of these primitive types has a corresponding primitive wrapper type, respectively, String, Number, and Boolean.
The JavaScript interpreter creates temporary objects of these primitive wrapper types as needed, uses them, and then discards them immediately. This is how properties and methods can seemingly be invoked on primitive types.
For example, extract the first character of a string using a method call.
var first = 'hello'.charAt(0);
** Functions are first-class objects.
That is, functions are objects. So functions are values that can be assigned to variables, passed as parameters, be returned, and appear in expressions, just like values of other types.
** There are two literal ways of defining functions.
The first is the familiar declaration that starts with the keyword function and must include a function name. For example,
function helloWorld() {
console.log("hello, world");
}
The second way is an expression that also starts with the keyword function but does not include a name. For example, this code defines an anonymous function and assigns it to a variable.
var helloWorld = function() {
console.log("hello, world");
}
** A function declaration may appear after a call to that function.
For example, this is allowed.
helloWorld();
function helloWorld() {
console.log("hello, world");
}
The function declaration is effectively hoisted to the top of its context.
But this only works for function declarations, not for function expressions. The code below results in a runtime error.
hello();
var hello = function() {
console.log("hello");
}
(Continued at Part 2.)
So far, it's been excellent, one of the best technical books I've read. It's clear, concise, and doesn't have any fluff, a solid tutorial with fewer than one hundred pages.
Coming from having programmed a lot in the strongly-typed language C++ a couple of lifetimes ago, I find JavaScript very fluid and sometimes very different in certain aspects.
It has some commonalities with PHP, which is what I'm mostly working in these days and still learning.
The purpose of this post and its follow-up is to highlight those JavaScript features described in this book that might be of particular interest to someone with a background similar to mine.
** Javascript does not have classes nor class definitions in the usual sense.
It does have objects, which are instances of reference types. The built-in reference types include Object, Array, Date, Function, RegExp, and Error.
** An object is a dynamic collection of properties and their values.
Properties can be added or removed from an object at any time. In other words, by default, an object is not constrained by whatever type was used to instantiate it.
For example, create a Date object, then add an arbitrary property to it. Note that assigning a value to a property that didn't exist before implicitly adds that property to the object.
var date = new Date;
date.blessing = 'May you live in interesting times';
Then delete the property. The date object is now back to its initial state.
delete date.blessing;
** The primitive types, which include string, number, and boolean, sometimes can be treated as objects even though they are not objects.
Each of these primitive types has a corresponding primitive wrapper type, respectively, String, Number, and Boolean.
The JavaScript interpreter creates temporary objects of these primitive wrapper types as needed, uses them, and then discards them immediately. This is how properties and methods can seemingly be invoked on primitive types.
For example, extract the first character of a string using a method call.
var first = 'hello'.charAt(0);
** Functions are first-class objects.
That is, functions are objects. So functions are values that can be assigned to variables, passed as parameters, be returned, and appear in expressions, just like values of other types.
** There are two literal ways of defining functions.
The first is the familiar declaration that starts with the keyword function and must include a function name. For example,
function helloWorld() {
console.log("hello, world");
}
The second way is an expression that also starts with the keyword function but does not include a name. For example, this code defines an anonymous function and assigns it to a variable.
var helloWorld = function() {
console.log("hello, world");
}
** A function declaration may appear after a call to that function.
For example, this is allowed.
helloWorld();
function helloWorld() {
console.log("hello, world");
}
The function declaration is effectively hoisted to the top of its context.
But this only works for function declarations, not for function expressions. The code below results in a runtime error.
hello();
var hello = function() {
console.log("hello");
}
(Continued at Part 2.)
Apr 25, 2015
Beta 9: automated tests, module_invoke(), and drush
When I tried to run the automated tests, none of them showed up in the list of available tests for the Testing module's configuration.
It turns out that there is a new requirement for writing test classes, namely,
/**
* Create users with no, some, and optimizely permissions
* to test access to module related pages.
*
* @group Optimizely
*/
class OptimizelyAccessTest extends WebTestBase { . . . . }
This @group annotation seems to have been created specifically for the Testing module and is not part of the general documentation standards.
Sources:
Automated tests
https://api.drupal.org/api/drupal/core!modules!system!core.api.php/group/testing/8
API documentation and comment standards
https://www.drupal.org/coding-standards/docs
Fatal error: Call to undefined function Drupal\optimizely\Tests\module_invoke()
Function module_invoke() has been removed. I replaced this call,
$schema = module_invoke('optimizely', 'schema');
with this one.
$schema = \Drupal::moduleHandler()
->invoke('optimizely', 'schema');
Generally speaking, interface ModuleHandlerInterface and class ModuleHandler provide the functionality that was previously in functions such as this one as well as module_list() and module_implements() which have also been removed.
Sources:
Remove bootstrap.inc module_invoke()
https://www.drupal.org/node/2330181
Module/hook system functions replaced with module_handler and module_installer
https://www.drupal.org/node/1894902
Running drush on a freshly-installed Beta 9 site gave the error message,
Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container. in Drupal::getContainer() (line 129 of /var/www/html/opti/core/lib/Drupal.php).
I was using a version of drush 7 from January. Downloading and installing the current drush 7.0-dev fixed this problem.
Sources:
Installing Drush for Drupal 8
http://optimizely-to-drupal-8.blogspot.com/2015/01/installing-drush-for-drupal-8.html
It turns out that there is a new requirement for writing test classes, namely,
Your test class needs a phpDoc comment block with a description and a @group annotation, which gives information about the test.So, for example, by adding @group in the following comment block, the test class was recognized and listed under the Optimizely grouping. The lead paragraph of the comment block is treated as a description and is displayed accordingly in the listing.
/**
* Create users with no, some, and optimizely permissions
* to test access to module related pages.
*
* @group Optimizely
*/
class OptimizelyAccessTest extends WebTestBase { . . . . }
This @group annotation seems to have been created specifically for the Testing module and is not part of the general documentation standards.
Sources:
Automated tests
https://api.drupal.org/api/drupal/core!modules!system!core.api.php/group/testing/8
API documentation and comment standards
https://www.drupal.org/coding-standards/docs
Fatal error: Call to undefined function Drupal\optimizely\Tests\module_invoke()
Function module_invoke() has been removed. I replaced this call,
$schema = module_invoke('optimizely', 'schema');
with this one.
$schema = \Drupal::moduleHandler()
->invoke('optimizely', 'schema');
Generally speaking, interface ModuleHandlerInterface and class ModuleHandler provide the functionality that was previously in functions such as this one as well as module_list() and module_implements() which have also been removed.
Sources:
Remove bootstrap.inc module_invoke()
https://www.drupal.org/node/2330181
Module/hook system functions replaced with module_handler and module_installer
https://www.drupal.org/node/1894902
Running drush on a freshly-installed Beta 9 site gave the error message,
Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container. in Drupal::getContainer() (line 129 of /var/www/html/opti/core/lib/Drupal.php).
I was using a version of drush 7 from January. Downloading and installing the current drush 7.0-dev fixed this problem.
Sources:
Installing Drush for Drupal 8
http://optimizely-to-drupal-8.blogspot.com/2015/01/installing-drush-for-drupal-8.html
Apr 6, 2015
"An Introduction to Entities"
An Introduction to Entities
https://www.drupal.org/node/1261744
I might be particularly dense about this aspect of Drupal, but this is the first article I've read that clearly and succinctly describes the closely related concepts of entity type, bundle, node, content type, field, and entity.
For those who are familiar with object-oriented programming, there is a comparison that is helpful, even though the analogy is not a perfect match.
Kudos to the authors.
https://www.drupal.org/node/1261744
I might be particularly dense about this aspect of Drupal, but this is the first article I've read that clearly and succinctly describes the closely related concepts of entity type, bundle, node, content type, field, and entity.
For those who are familiar with object-oriented programming, there is a comparison that is helpful, even though the analogy is not a perfect match.
Kudos to the authors.
Subscribe to:
Posts (Atom)