You are here

API.txt in Spaces 6.3

Same filename and directory in other branches
  1. 7.3 API.txt
  2. 7 API.txt
Spaces 3.x API
--------------
The following is an overview of using the Spaces API.


Basic usage & concepts
----------------------
This section is for developers who are using Spaces simply in the context of
site building and don't plan to create any new space types or controllers
(...yet).


### The active space

On a single page load, only one space (or no space) may be active. Spaces can
be activated through a variety of means, e.g. PURL callback or `hook_init()`,
but they generally must happen early enough in the Drupal bootstrap to be before
the main page callback is executed.

What a space activation looks like in code:

    // Loads space class for user uid = 5.
    $space = spaces_load('user', 5);

    // Makes this the active space for the page.
    $space->activate();

You can retrieve the active space later in the page load at any point:

    // Retrieve the active space, and use it to set the page title.
    $space = spaces_get_space();
    drupal_set_title($space->title());


### Spaces that use PURL

Two of the space types included in Spaces make use of PURL for their activation
(OG, taxonomy). When working with these spaces, you will want to be familiar
with the PURL API for doing things like redirecting between spaces, creating
links that leave or enter a space, and so forth. Please consult the PURL
`README.txt`.


### Checking access

Each space can have a different configuration for how features and settings can
be accessed. For example, group spaces for private groups (`og_private`) require
that a user is a member of that group before she may access any of its features.

You can check for feature access on a specific space:

    // Check that the current user can view the spaces_blog feature for this
    // space
    $space->access_feature('view', 'space_blog');

    // Check that the current user can create a node type associated with
    // spaces_blog feature for this space
    $space->access_feature('create', 'spaces_blog');

    // Function wrapper around performing equivalent checks for the current
    // active space, useful for menu access callbacks
    spaces_access_feature('view', 'spaces_blog');

Other methods for similar checks can be provided by each space type. In each
pair, the first can be run against any space object while the
equivalent will call the equivalent method for only the current active space.

- `$space->access_space()` and `spaces_access_space()`: Can the current user
  access view this space at all when it is active?
- `$space->access_admin()` and `spaces_access_admin()`: Can the current user
  administer this space, e.g. enable or disable features.
- `$space->access_user()` and `spaces_access_user()`: Can the current user view
  the specified user account in this space?


### Using controllers

Much of Spaces overrides are handled transparently in Drupal UI elements and API
functions. However, some times you need to get at a specific value or check
whether, for example, the value you got from `variable_get()` originated from
the space, its preset, or the site. Controllers provides methods for doing this.

The space object will have all of the available controller plugins available
under `$space->controllers`. Each controller provides three methods:


`$controller->get($id = NULL, $environment = NULL)`

- `$id` refers to the name of the object you're getting. Example:
  `site_frontpage`.
- `$environment` may be either `original`, `preset`, or `space` or `NULL`. If an
  environment is specified, the value for that specific environment will
  be used. If no environment is provided, the controller will perform a
  cascade (e.g. if there is no space value, preset will be checked, if there is
  no preset value, original will be checked.)

Examples:

    // Get an inherited value for the 'site_frontpage' variable.
    $space->controllers->variable->get('site_frontpage');

    // Get this space's override value for 'site_frontpage'.
    $space->controllers->variable->get('site_frontpage', 'space');

    // Get all contexts in this space's preset.
    $space->controllers->context->get(NULL, 'preset');


`$controller->set($id, $value)`

- `$id` refers to the name of the object you'd like to override for this space.
  Example: `site_frontpage`.
- `$value` is the value that you would like to set for this object. Example:
 `node`.


Examples:

    // Set the site frontpage to 'dashboard' for this space.
    $space->controllers->variable->set('site_frontpage', 'dashboard');

    // Set a context override for this space.
    $context = context_load('foo');
    $space->controllers->context->set('bar', $context);


`$controller->del($id)`

- `$id` refers to the name of the override you'd like to remove in this space.
  Example: `site_frontpage`.

Examples:

    // Clear out the site frontpage override for this space.
    $space->controllers->variable->del('site_frontpage');


Adding a space type or controller plugin
----------------------------------------
Both space types and controllers utilize the CTools plugins API. In order
to add a new plugin fro your module, follow these steps:

1. Implement `hook_ctools_plugin_api()` in your module. This declares that your
  module is API compatible with Spaces 3.

        function mymodule_ctools_plugin_api($module, $api) {
          if ($module == 'spaces' && $api == 'plugins') {
            return array('version' => 3);
          }
        }

2. Implement `hook_spaces_plugins()` to define your plugins, classes, and class
  hierarchy.

        function mymodule_spaces_plugins() {
          $plugins = array();
          $plugins['mymodule_space_foo'] = array(
            'handler' => array(
              'path' => drupal_get_path('module', 'mymodule') .'/plugins',
              'file' => 'mymodule_space_foo.inc',
              'class' => 'mymodule_space_foo',
              'parent' => 'space',
            ),
          );
          return $plugins;
        }

3. Implement `hook_spaces_registry()` to define your space types and/or
  controllers and map them to plugins.

        function mymodule_spaces_registry() {
          return array(
            'types' => array(
              'foo' => array(
                'title' => t('The foo space'),
                'plugin' => 'mymodule_space_foo',
              ),
            ),
          );
        }

4. Write your space type or controller plugin class. It's best to look at one of
  the included plugins as a starting point.


Replacing or extending existing plugins
---------------------------------------
You can replace a space or controller plugin with your own plugin class using
`hook_spaces_registry_alter()`:

    function mymodule_spaces_registry_alter(&$registry) {
      if (!empty($registry['types']['og'])) {
        $registry['type']['og']['plugin'] = 'mymodule_space_og_improved';
      }
    }

This entry would swap out the default og space type plugin for a custom one
provided by `mymodule`. Note that any replacement plugins must have an entry in
`hook_spaces_plugins()`.

File

API.txt
View source
  1. Spaces 3.x API
  2. --------------
  3. The following is an overview of using the Spaces API.
  4. Basic usage & concepts
  5. ----------------------
  6. This section is for developers who are using Spaces simply in the context of
  7. site building and don't plan to create any new space types or controllers
  8. (...yet).
  9. ### The active space
  10. On a single page load, only one space (or no space) may be active. Spaces can
  11. be activated through a variety of means, e.g. PURL callback or `hook_init()`,
  12. but they generally must happen early enough in the Drupal bootstrap to be before
  13. the main page callback is executed.
  14. What a space activation looks like in code:
  15. // Loads space class for user uid = 5.
  16. $space = spaces_load('user', 5);
  17. // Makes this the active space for the page.
  18. $space->activate();
  19. You can retrieve the active space later in the page load at any point:
  20. // Retrieve the active space, and use it to set the page title.
  21. $space = spaces_get_space();
  22. drupal_set_title($space->title());
  23. ### Spaces that use PURL
  24. Two of the space types included in Spaces make use of PURL for their activation
  25. (OG, taxonomy). When working with these spaces, you will want to be familiar
  26. with the PURL API for doing things like redirecting between spaces, creating
  27. links that leave or enter a space, and so forth. Please consult the PURL
  28. `README.txt`.
  29. ### Checking access
  30. Each space can have a different configuration for how features and settings can
  31. be accessed. For example, group spaces for private groups (`og_private`) require
  32. that a user is a member of that group before she may access any of its features.
  33. You can check for feature access on a specific space:
  34. // Check that the current user can view the spaces_blog feature for this
  35. // space
  36. $space->access_feature('view', 'space_blog');
  37. // Check that the current user can create a node type associated with
  38. // spaces_blog feature for this space
  39. $space->access_feature('create', 'spaces_blog');
  40. // Function wrapper around performing equivalent checks for the current
  41. // active space, useful for menu access callbacks
  42. spaces_access_feature('view', 'spaces_blog');
  43. Other methods for similar checks can be provided by each space type. In each
  44. pair, the first can be run against any space object while the
  45. equivalent will call the equivalent method for only the current active space.
  46. - `$space->access_space()` and `spaces_access_space()`: Can the current user
  47. access view this space at all when it is active?
  48. - `$space->access_admin()` and `spaces_access_admin()`: Can the current user
  49. administer this space, e.g. enable or disable features.
  50. - `$space->access_user()` and `spaces_access_user()`: Can the current user view
  51. the specified user account in this space?
  52. ### Using controllers
  53. Much of Spaces overrides are handled transparently in Drupal UI elements and API
  54. functions. However, some times you need to get at a specific value or check
  55. whether, for example, the value you got from `variable_get()` originated from
  56. the space, its preset, or the site. Controllers provides methods for doing this.
  57. The space object will have all of the available controller plugins available
  58. under `$space->controllers`. Each controller provides three methods:
  59. `$controller->get($id = NULL, $environment = NULL)`
  60. - `$id` refers to the name of the object you're getting. Example:
  61. `site_frontpage`.
  62. - `$environment` may be either `original`, `preset`, or `space` or `NULL`. If an
  63. environment is specified, the value for that specific environment will
  64. be used. If no environment is provided, the controller will perform a
  65. cascade (e.g. if there is no space value, preset will be checked, if there is
  66. no preset value, original will be checked.)
  67. Examples:
  68. // Get an inherited value for the 'site_frontpage' variable.
  69. $space->controllers->variable->get('site_frontpage');
  70. // Get this space's override value for 'site_frontpage'.
  71. $space->controllers->variable->get('site_frontpage', 'space');
  72. // Get all contexts in this space's preset.
  73. $space->controllers->context->get(NULL, 'preset');
  74. `$controller->set($id, $value)`
  75. - `$id` refers to the name of the object you'd like to override for this space.
  76. Example: `site_frontpage`.
  77. - `$value` is the value that you would like to set for this object. Example:
  78. `node`.
  79. Examples:
  80. // Set the site frontpage to 'dashboard' for this space.
  81. $space->controllers->variable->set('site_frontpage', 'dashboard');
  82. // Set a context override for this space.
  83. $context = context_load('foo');
  84. $space->controllers->context->set('bar', $context);
  85. `$controller->del($id)`
  86. - `$id` refers to the name of the override you'd like to remove in this space.
  87. Example: `site_frontpage`.
  88. Examples:
  89. // Clear out the site frontpage override for this space.
  90. $space->controllers->variable->del('site_frontpage');
  91. Adding a space type or controller plugin
  92. ----------------------------------------
  93. Both space types and controllers utilize the CTools plugins API. In order
  94. to add a new plugin fro your module, follow these steps:
  95. 1. Implement `hook_ctools_plugin_api()` in your module. This declares that your
  96. module is API compatible with Spaces 3.
  97. function mymodule_ctools_plugin_api($module, $api) {
  98. if ($module == 'spaces' && $api == 'plugins') {
  99. return array('version' => 3);
  100. }
  101. }
  102. 2. Implement `hook_spaces_plugins()` to define your plugins, classes, and class
  103. hierarchy.
  104. function mymodule_spaces_plugins() {
  105. $plugins = array();
  106. $plugins['mymodule_space_foo'] = array(
  107. 'handler' => array(
  108. 'path' => drupal_get_path('module', 'mymodule') .'/plugins',
  109. 'file' => 'mymodule_space_foo.inc',
  110. 'class' => 'mymodule_space_foo',
  111. 'parent' => 'space',
  112. ),
  113. );
  114. return $plugins;
  115. }
  116. 3. Implement `hook_spaces_registry()` to define your space types and/or
  117. controllers and map them to plugins.
  118. function mymodule_spaces_registry() {
  119. return array(
  120. 'types' => array(
  121. 'foo' => array(
  122. 'title' => t('The foo space'),
  123. 'plugin' => 'mymodule_space_foo',
  124. ),
  125. ),
  126. );
  127. }
  128. 4. Write your space type or controller plugin class. It's best to look at one of
  129. the included plugins as a starting point.
  130. Replacing or extending existing plugins
  131. ---------------------------------------
  132. You can replace a space or controller plugin with your own plugin class using
  133. `hook_spaces_registry_alter()`:
  134. function mymodule_spaces_registry_alter(&$registry) {
  135. if (!empty($registry['types']['og'])) {
  136. $registry['type']['og']['plugin'] = 'mymodule_space_og_improved';
  137. }
  138. }
  139. This entry would swap out the default og space type plugin for a custom one
  140. provided by `mymodule`. Note that any replacement plugins must have an entry in
  141. `hook_spaces_plugins()`.