- <?php
- * @file
- * Hook implementations for the Node Type Example module.
- *
- * Part of the Examples for Developers project.
- */
-
- * @defgroup node_type_example Example: Node type
- * @ingroup examples
- * @{
- * This example demonstrates how to programmatically define a content type.
- *
- * This is an example outlining how a module can define a new content type. The
- * example content type allows users to specify multiple colors, a quantity, and
- * an image. It could be used as some kind of rudimentary inventory-tracking
- * system.
- *
- * The basic pattern for defining a content type is to tell Backdrop about the
- * node fields and view modes. Backdrop will then take over and manage the
- * storage for this content type.
- *
- * Remember that most content types do not require any custom code, as they are
- * usually created using the Backdrop user interface.
- *
- * At absolute minimum, in order to provide a content type, you have to
- * implement hook_form(). The Node module can take care of the rest, if you
- * want.
- *
- * Next, since we want to add fields to our content type, we implement
- * hook_node_type_insert(), which gives us a chance to modify recently-created
- * content types.
- *
- * Backdrop is able to handle deletion of our content, including dependencies
- * based on re-use of our field instances, so we don't have to manage any of it.
- *
- * Consult the @link field Field API @endlink documentation.
- *
- * @see field_example.module
- */
-
- * Implements hook_menu().
- *
- * We are providing a default page to illustrate the use of our custom node view
- * mode that will live at http://example.com/?q=examples/node_type_example.
- */
- function node_type_example_menu() {
- $items['examples/node_type_example'] = array(
- 'page callback' => 'node_type_example_page',
- 'access arguments' => array('access content'),
- 'title' => 'Node type example',
- );
- return $items;
- }
-
- * Implements hook_node_type_insert().
- *
- * Much like hook_node_insert() lets us know that a node is being inserted into
- * the database, hook_node_type_insert() lets us know that a new content type
- * has been inserted.
- *
- * Since Backdrop will at some point insert our new content type, this gives us
- * a chance to add the fields we want.
- *
- * It is called for all inserts to the content type database, so we have to make
- * sure we're only modifying the type we're concerned with.
- */
- function node_type_example_node_type_insert($content_type) {
- if ($content_type->type == 'node_type_example') {
-
-
-
- $body_instance = node_add_body_field($content_type, t('Example description'));
-
-
-
- $body_instance['display']['node_type_example_list'] = array(
- 'label' => 'hidden',
- 'type' => 'text_summary_or_trimmed',
- );
-
-
- field_update_instance($body_instance);
-
-
- foreach (_node_type_example_installed_fields() as $field) {
- field_create_field($field);
- }
-
-
- foreach (_node_type_example_installed_instances() as $instance) {
- $instance['entity_type'] = 'node';
- $instance['bundle'] = 'node_type_example';
- field_create_instance($instance);
- }
- }
- }
-
- * Implements hook_form().
- *
- * Backdrop needs for us to provide a form that lets the user add content. This
- * is the form users will see if they go to node/add/node-example.
- *
- * You can get fancy with this form, or you can just punt and return the default
- * form node_content_form() provides.
- */
- function node_type_example_form($node, $form_state) {
- return node_content_form($node, $form_state);
- }
-
- * Callback that builds our content.
- *
- * @return array
- * A renderable array showing a list of our nodes.
- *
- * @see node_load()
- * @see node_view()
- * @see node_type_example_field_formatter_view()
- */
- function node_type_example_page() {
- $renderable_array = array();
-
- $sql = 'SELECT nid FROM {node} n WHERE n.type = :type AND n.status = :status';
- $result = db_query($sql, array(
- ':type' => 'node_type_example',
- ':status' => 1)
- );
- $renderable_array['explanation'] = array(
- '#markup' => t("The created node_type_example nodes will be displayed here. The color fields will be displayed differently in this list than when you normally view nodes. Click on the node title to see the difference. This is the result of using our 'node_type_example_list' node view type."),
- );
-
-
-
- foreach ($result as $row) {
- $node = node_load($row->nid);
- $renderable_array['node_list'][] = node_view($node, 'node_type_example_list');
- }
- return $renderable_array;
- }
-
- * Implements hook_entity_info_alter().
- *
- * We need to modify the default node entity info by adding a new view mode to
- * be used in functions like node_view() or node_build_content().
- */
- function node_type_example_entity_info_alter(&$entity_info) {
-
- $entity_info['node']['view modes']['node_type_example_list'] = array(
- 'label' => t('Example node list'),
- 'custom settings' => TRUE,
- );
- }
-
-
- * Implements hook_field_formatter_info().
- */
- function node_type_example_field_formatter_info() {
- return array(
- 'node_type_example_colors' => array(
- 'label' => t('Node color handle'),
- 'field types' => array('text'),
- ),
- );
- }
-
- * Implements hook_field_formatter_view().
- *
- * @todo Provide a formatter for the colors.
- */
- function node_type_example_field_formatter_view($object_type, $object, $field, $instance, $langcode, $items, $display) {
- $element = array();
-
- if ($display['type'] == 'node_type_example_colors') {
- foreach ($items as $delta => $item) {
- $element[$delta]['#type'] = 'markup';
- $color = $item['safe_value'];
- $element[$delta]['#markup'] = theme('node_type_example_color', array('color' => $color));
- }
- }
-
- return $element;
- }
-
- * Implements hook_theme().
- *
- * It gives to Backdrop the list of our theme functions and their arguments.
- */
- function node_type_example_theme($existing, $type, $theme, $path) {
- return array(
- 'node_type_example_color' => array(
- 'variables' => array('color' => NULL),
- ),
- );
- }
-
- * A custom theme function.
- *
- * By using this function to format our node-specific information, themes can
- * override this presentation if they wish. This is a simplified theme function
- * purely for illustrative purposes.
- */
- function theme_node_type_example_color($variables) {
- $output = '<span style="background-color: #ccc; padding: 1em; margin-bottom: 1em; float: left; color: ' . $variables['color'] . '">' . $variables['color'] . '</span>';
- return $output;
- }
-
- * Defines the fields for our content type.
- *
- * @return array
- * An associative array specifying the fields we wish to add to our new
- * content type.
- */
- function _node_type_example_installed_fields() {
- return array(
- 'node_type_example_color' => array(
- 'field_name' => 'node_type_example_color',
- 'cardinality' => 3,
- 'type' => 'text',
- 'settings' => array(
- 'max_length' => 60,
- ),
- ),
- 'node_type_example_quantity' => array(
- 'field_name' => 'node_type_example_quantity',
- 'cardinality' => 1,
- 'type' => 'text',
- ),
- 'node_type_example_image' => array(
- 'field_name' => 'node_type_example_image',
- 'type' => 'image',
- 'cardinality' => 1,
- ),
- );
- }
-
- * Defines the field instances for our content type.
- *
- * It lets Backdrop know which widget to use to allow users to enter data and
- * how to react in different view modes. We are going to display a page that
- * uses a custom "node_type_example_list" view mode. We will set the cardinality
- * to three, allowing our content type to give users three color fields.
- *
- * @return array
- * An associative array specifying the instances we wish to add to our new
- * content type.
- */
- function _node_type_example_installed_instances() {
- return array(
- 'node_type_example_color' => array(
- 'field_name' => 'node_type_example_color',
- 'label' => t('The colors available for this object.'),
- 'widget' => array(
- 'type' => 'text_textfield',
- ),
- 'display' => array(
- 'node_type_example_list' => array(
- 'label' => 'hidden',
- 'type' => 'node_type_example_colors',
- ),
- ),
- ),
- 'node_type_example_quantity' => array(
- 'field_name' => 'node_type_example_quantity',
- 'label' => t('Quantity required'),
- 'type' => 'text',
- 'widget' => array(
- 'type' => 'text_textfield',
- ),
- 'display' => array(
- 'node_type_example_list' => array(
- 'label' => 'hidden',
- 'type' => 'hidden',
- ),
- ),
- ),
- 'node_type_example_image' => array(
- 'field_name' => 'node_type_example_image',
- 'label' => t('Upload an image:'),
- 'required' => FALSE,
- 'widget' => array(
- 'type' => 'image_image',
- 'weight' => 2.10,
- ),
- 'display' => array(
- 'node_type_example_list' => array(
- 'label' => 'hidden',
- 'type' => 'image_link_content__thumbnail',
- ),
- ),
- ),
- );
- }
-
- * @} End of "defgroup node_type_example".
- */