diff --git a/README.txt b/README.txt index 23ac597..a282e61 100644 --- a/README.txt +++ b/README.txt @@ -161,20 +161,24 @@ admin/config/search/metatags/settings required otherwise none of the meta tag fields will display at all. The functionality may be disabled again by either removing the variable or setting it to FALSE. -* It's possible to disable Metatag integration for certain entity types or - bundles using variables. To disable an entity just assigning a variable - 'metatag_enable_{$entity_type}' or 'metatag_enable_{$entity_type}__{$bundle}' - the value FALSE, e.g.: - // Disable metatags for file_entity. +* Each entity type (nodes, terms, users, etc) & bundle (content types, + vocabularies, etc) may have its Metatag integration enabled & disabled from + the Advanced Settings page. + These UI options correspond to variables. To enable an entity or bundle just + assign a variable 'metatag_enable_{$entity_type}' or + 'metatag_enable_{$entity_type}__{$bundle}' the value FALSE, e.g.: + // Disable metatags for files (file_entity module). $conf['metatag_enable_file'] = FALSE; - // Disable metatags for carousel nodes. + // Disable metatags for carousel nodes, but leave it enabled for all other + // content types. $conf['metatag_enable_node__carousel'] = FALSE; To enable the entity and/or bundle simply set the value to TRUE or remove the settings.php line. Note that the Metatag cache will need to be cleared after changing these settings, specifically the 'info' records, e.g., 'info:en'; a quick version of doing this is to clear the site caches using either Drush, - Admin Menu (flush all caches), or the "Clear all caches" button on - admin/config/development/performance. + Admin Menu (flush all caches -> Metatag), or the "Clear all caches" button on + admin/config/development/performance. Changing these from the Advanced + Settings page automatically clears the cache. * By default Metatag will not display meta tags on admin pages. To enable meta tags on admin pages simply set the 'metatag_tag_admin_pages' variable to TRUE through one of the following methods: @@ -209,9 +213,9 @@ Developers ------------------------------------------------------------------------------ Full API documentation is available in metatag.api.php. -To enable Metatag support in custom entities, add 'metatag' => TRUE to either -the entity or bundle definition in hook_entity_info(); see metatag.api.php for -further details and example code. +It is not necessary to control Metatag via the entity API, any entity that has +view modes defined and is not a configuration entity is automatically suitable +for use. The meta tags for a given entity object (node, etc) can be obtained as follows: $metatags = metatags_get_entity_metatags($entity_id, $entity_type, $langcode); diff --git a/metatag.admin.inc b/metatag.admin.inc index 3fd636a..66f871a 100644 --- a/metatag.admin.inc +++ b/metatag.admin.inc @@ -188,6 +188,7 @@ function metatag_config_overview() { drupal_get_path('module', 'metatag') . '/metatag.admin.css', ), ), + '#suffix' => '

' . t('Any items marked "Unknown" are configurations in the system for entity types or bundles which have been disabled via the API or the Advanced Settings page; they will not be used.', array('@url' => url('admin/config/search/metatags/settings'))) . '

', ); return $build; @@ -612,45 +613,41 @@ function metatag_admin_settings_form() { $form['entities'] = array( '#type' => 'fieldset', '#title' => t('Master controls for all entities'), - '#description' => t('By enabling and disabling items here, it is possible to control which entities (e.g. nodes, taxonomy terms) and bundles (e.g. content types, vocabularies) will have meta tags available to them.
Note: the entities first have to have meta tags enabled via hook_entity_info; see the API documentation for full details.'), + '#description' => t('By enabling and disabling items here, it is possible to control which entities (e.g. nodes, taxonomy terms) and bundles (e.g. content types, vocabularies) will have meta tags available to them.
Technical note: Entity types must not be configuration entities and must have view modes in order to be compatible.'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); - foreach (entity_get_info() as $entity_name => $entity_info) { - // Only show entities that have been enabled via the hooks. - if (!empty($entity_info['metatags'])) { - $form['entities']['metatag_enable_' . $entity_name] = array( + foreach (entity_get_info() as $entity_type => $entity_info) { + // Only show entities that are capable of using meta tags. + if (metatag_entity_type_is_suitable($entity_type, $entity_info)) { + $form['entities']['metatag_enable_' . $entity_type] = array( '#type' => 'checkbox', '#title' => t($entity_info['label']), - '#default_value' => variable_get('metatag_enable_' . $entity_name, TRUE), + '#default_value' => metatag_entity_supports_metatags($entity_type), '#description' => t('Enable meta tags for all pages this entity type.'), ); - if (!empty($entity_info['bundles'])) { + // Some entities, e.g. User, (core) File, have a single bundle with the + // same name as the entity, so only show the bundles list if there is + // more than one of them. + if (!empty($entity_info['bundles']) && count($entity_info['bundles']) > 1) { $desc_added = FALSE; foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { - // Some entities, e.g. User, (core) File, have a bundle with the same - // name as the entity, so - if ($bundle_name != $entity_name) { - // Add an extra line to the description introducing the entity - // bundles. - if (!$desc_added) { - $form['entities']['metatag_enable_' . $entity_name]['#disabled'] = TRUE; - $form['entities']['metatag_enable_' . $entity_name]['#default_value'] = TRUE; - $form['entities']['metatag_enable_' . $entity_name]['#description'] = t('Each bundle for this entity must be controlled individually.'); - $desc_added = TRUE; - } - $form['entities']['metatag_enable_' . $entity_name . '__' . $bundle_name] = array( - '#type' => 'checkbox', - '#title' => t($bundle_info['label']), - '#default_value' => variable_get('metatag_enable_' . $entity_name . '__' . $bundle_name, isset($bundle_info['metatags']) ? $bundle_info['metatags'] : TRUE), - '#attributes' => array( - // Add some theming that'll indent this bundle. - 'class' => array('metatag-bundle-checkbox'), + $form['entities']['metatag_enable_' . $entity_type . '__' . $bundle_name] = array( + '#type' => 'checkbox', + '#title' => t($bundle_info['label']), + '#default_value' => metatag_entity_supports_metatags($entity_type, $bundle_name), + '#attributes' => array( + // Add some theming that'll indent this bundle. + 'class' => array('metatag-bundle-checkbox'), + ), + '#states' => array( + 'visible' => array( + ':input[name="metatag_enable_' . $entity_type . '"]' => array('checked' => TRUE), ), - ); - } + ), + ); } } } diff --git a/metatag.api.php b/metatag.api.php index b8e6007..b51111a 100644 --- a/metatag.api.php +++ b/metatag.api.php @@ -5,67 +5,6 @@ */ /** - * To enable Metatag support in custom entities, add 'metatags' => TRUE to the - * entity definition in hook_entity_info(), e.g.: - * - * /** - * * Implements hook_entity_info(). - * * - * * Taken from the Examples module. - * * / - * function entity_example_entity_info() { - * $info['entity_example_basic'] = array( - * 'label' => t('Example Basic Entity'), - * 'controller class' => 'EntityExampleBasicController', - * 'base table' => 'entity_example_basic', - * 'uri callback' => 'entity_example_basic_uri', - * 'fieldable' => TRUE, - * 'metatags' => TRUE, - * 'entity keys' => array( - * 'id' => 'basic_id' , // The 'id' (basic_id here) is the unique id. - * 'bundle' => 'bundle_type' // Bundle will be determined by the 'bundle_type' field - * ), - * 'bundle keys' => array( - * 'bundle' => 'bundle_type', - * ), - * 'static cache' => TRUE, - * 'bundles' => array( - * 'first_example_bundle' => array( - * 'label' => 'First example bundle', - * 'admin' => array( - * 'path' => 'admin/structure/entity_example_basic/manage', - * 'access arguments' => array('administer entity_example_basic entities'), - * ), - * ), - * ), - * 'view modes' => array( - * 'tweaky' => array( - * 'label' => t('Tweaky'), - * 'custom settings' => FALSE, - * ), - * ) - * ); - * - * return $info; - * } - * - * The definition of each bundle may be handled separately, thus support may be - * disabled for the entity as a whole but enabled for individual bundles. This - * is handled via the 'metatags' value on the bundle definition, e.g.: - * - * 'bundles' => array( - * 'first_example_bundle' => array( - * 'label' => 'First example bundle', - * 'metatags' => TRUE, - * 'admin' => array( - * 'path' => 'admin/structure/entity_example_basic/manage', - * 'access arguments' => array('administer entity_example_basic entities'), - * ), - * ), - * ), - */ - -/** * Provides a default configuration for Metatag intances. * * This hook allows modules to provide their own Metatag instances which can diff --git a/metatag.install b/metatag.install index 4d17a96..31f0098 100644 --- a/metatag.install +++ b/metatag.install @@ -257,6 +257,16 @@ function metatag_schema() { */ function metatag_install() { drupal_set_message(t("Thank you for installing the Metatag module. It is recommended to read the module's README.txt file as there are some known issues that may affect this site.", array('!url' => url(drupal_get_path('module', 'metatag') . '/README.txt')))); + + // Disable Metatag support for some entities that don't usually need it. + variable_set('metatag_enable_comment', FALSE); + variable_set('metatag_enable_fieldable_panels_pane', FALSE); + variable_set('metatag_enable_file', FALSE); + + // Enable Metatag support for entity types that usally expect it. + variable_set('metatag_enable_node', TRUE); + variable_set('metatag_enable_taxonomy_term', TRUE); + variable_set('metatag_enable_user', TRUE); } /** @@ -289,6 +299,16 @@ function metatag_uninstall() { // Optionally disables the output cache. variable_del('metatag_cache_output'); + + // Remove all possible 'enable' variables. + foreach (entity_info() as $entity_type => $entity_info) { + variable_del('metatag_enable_' . $entity_type); + if (!empty($entity_info['bundles'])) { + foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { + variable_del('metatag_enable_' . $entity_type . '__' . $bundle_name); + } + } + } } /** @@ -1558,3 +1578,63 @@ function metatag_update_7034() { } function metatag_update_7035() { } + +/** + * Update variables to indicate which entities should be supported. + */ +function metatag_update_7036() { + foreach (entity_info() as $entity_type => $entity_info) { + $variable_name = 'metatag_enable_' . $entity_type; + + // Configuration entities are skipped. + if (isset($entity_info['configuration']) && $entity_info['configuration'] == TRUE) { + continue; + } + + // Entities without view modes are skipped. + elseif (empty($entity_info['view modes'])) { + variable_set($variable_name, FALSE); + } + + // "Normal" entities that have been enabled via the API. + elseif (!empty($entity_info['metatag']) || !empty($entity_info['metatags'])) { + // Check if the entity type has been enabled or disabled previously; if + // the variable equals a junk value then it was not previously set, + // therefore we'll set a default. + if (variable_get($variable_name, 'monkey') == 'monkey') { + // By default these entity types are enabled. + variable_set($variable_name, TRUE); + // Check each entity bundle. + if (!empty($entity_info['bundles'])) { + foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { + $variable_name = 'metatag_enable_' . $entity_type . '__' . $bundle_name; + // Check if the bundle has been enabled or disabled previously; if + // the variable equals a junk value then it was not previously set, + // therefore we'll set a default. + if (variable_get($variable_name, 'monkey') == 'monkey') { + if (!empty($bundle_info['metatag']) || !empty($bundle_info['metatags'])) { + variable_set($variable_name, TRUE); + } + else { + variable_set($variable_name, FALSE); + } + } + // This variable was set before. + else { + // Do nothing. + } + } + } + } + // This variable was set before. + else { + // Do nothing. + } + } + + // Disable this entity type. + else { + variable_set($variable_name, FALSE); + } + } +} diff --git a/metatag.migrate.inc b/metatag.migrate.inc index 1592038..690479d 100644 --- a/metatag.migrate.inc +++ b/metatag.migrate.inc @@ -81,13 +81,13 @@ function metatag_migrate_api() { */ class MigrateMetatagHandler extends MigrateDestinationHandler { + /** + * Identify a list of supported entity types. + */ public function __construct() { - $entity_types = array(); - foreach (entity_get_info() as $entity_type => $entity_info) { - if (isset($entity_info['metatags']) && !empty($entity_info['metatags'])) { - $entity_types[] = $entity_type; - } - } + $entity_types = metatag_entity_supports_metatags(); + $entity_types = array_filter($entity_types); + $entity_types = array_keys($entity_types); $this->registerTypes($entity_types); } diff --git a/metatag.module b/metatag.module index 6b99278..7894c41 100644 --- a/metatag.module +++ b/metatag.module @@ -664,22 +664,31 @@ function metatag_entity_load($entities, $entity_type) { if (metatag_entity_supports_metatags($entity_type)) { // Get the revision_ids. $revision_ids = array(); + + // Since some entities do not have revisions, set the vid to the id. foreach ($entities as $key => $entity) { - list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity); - $revision_id = intval($revision_id); - if (!empty($revision_id)) { + list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity); + + // Verify that each entity bundle supports Metatag. + if (metatag_entity_supports_metatags($entity_type, $bundle)) { + if (empty($revision_id)) { + $revision_id = $entity_id; + } $revision_ids[] = $revision_id; } } - // Load all meta tags for these entities. - $metatags = metatag_metatags_load_multiple($entity_type, array_keys($entities), $revision_ids); + // Don't proceed if there are no revision_ids. + if (!empty($revision_ids)) { + // Load all meta tags for these entities. + $metatags = metatag_metatags_load_multiple($entity_type, array_keys($entities), $revision_ids); - // Assign the metatag records for the correct revision ID. - foreach ($entities as $entity_id => $entity) { - list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity); - $revision_id = intval($revision_id); - $entities[$entity_id]->metatags = isset($metatags[$entity_id][$revision_id]) ? $metatags[$entity_id][$revision_id] : array(); + // Assign the metatag records for the correct revision ID. + foreach ($entities as $entity_id => $entity) { + list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity); + $revision_id = intval($revision_id); + $entities[$entity_id]->metatags = isset($metatags[$entity_id][$revision_id]) ? $metatags[$entity_id][$revision_id] : array(); + } } } } @@ -1344,81 +1353,78 @@ function metatag_entity_has_metatags($entity_type, $entity) { // forms or processing meta tags on entity view. $config_exists = &drupal_static(__FUNCTION__, array()); list( , , $bundle) = entity_extract_ids($entity_type, $entity); - // Do not pretend to have metatags when the bundle does not support them. - if (!metatag_entity_supports_metatags($entity_type, $bundle)) { - return FALSE; - } - $instance = "{$entity_type}:{$bundle}"; - if (!isset($config_exists[$instance])) { - // Check if the intstance or its parents (excluding global) are enabled. - $config_exists[$instance] = metatag_config_is_enabled($instance, TRUE, FALSE); - } - return !empty($config_exists[$instance]); + // Verify whether or not the entity type / bundle supports metatags. + return metatag_entity_supports_metatags($entity_type, $bundle); } /** * Check whether the requested entity type (and bundle) support metatag. * - * By default this will be FALSE, support has to be specifically enabled by - * assigning 'metatag' => TRUE within the hook_entity_info() definition for the - * entity. + * Support may be specifically enabled by assigning 'metatag' => TRUE or + * 'metatags' = TRUE within the hook_entity_info() definition for the entity, + * or it may be controlled via the Advanced Settings form. */ function metatag_entity_supports_metatags($entity_type = NULL, $bundle = NULL) { $entity_types = &drupal_static(__FUNCTION__); + // Identify which entities & bundles are supported the first time the + // function is called. if (!isset($entity_types)) { - $entity_types = array(); + foreach (entity_get_info() as $entity_name => $entity_info) { + // Verify that this entity type is suitable. + $entity_types[$entity_name] = metatag_entity_type_is_suitable($entity_name, $entity_info); + + // The entity type technically supports entities. + if (!empty($entity_types[$entity_name])) { + // Allow entities to be disabled by assigning a variable + // 'metatag_enable_{$entity_type}' the value FALSE, e.g.: + // + // // Disable metatags for file_entity. + // $conf['metatag_enable_file'] = FALSE; + // + // @see Advanced settings page. + if (variable_get('metatag_enable_' . $entity_name, 'monkey') == FALSE) { + $entity_types[$entity_name] = FALSE; + } + + // Check each bundle. + else { + $entity_types[$entity_name] = array(); + foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { + // Allow bundles to be disabled by assigning a variable + // 'metatag_enable_{$entity_type}__{$bundle}' the value FALSE, e.g.: + // + // // Disable metatags for carousel nodes. + // $conf['metatag_enable_node__carousel'] = FALSE; + // + // @see Advanced settings page. + if (variable_get('metatag_enable_' . $entity_name . '__' . $bundle_name, 'monkey') == FALSE) { + $entity_types[$entity_name][$bundle_name] = FALSE; + } + else { + $entity_types[$entity_name][$bundle_name] = TRUE; + } + } + } + } + } } - // Everything else depends upon a specific entity type being checked. + // It was requested to check a specific entity. if (isset($entity_type)) { - if (!isset($entity_types[$entity_type])) { - $entity_info = entity_get_info($entity_type); - if (empty($entity_info['metatags'])) { - $entity_types[$entity_type] = FALSE; - } - else { - $entity_types[$entity_type] = array(); - foreach ($entity_info['bundles'] as $bundle_key => $bundle_info) { - $entity_types[$entity_type][$bundle_key] = !isset($bundle_info['metatags']) || !empty($bundle_info['metatags']); - } - } - } - - // If a specific entity bundle is being compared, check it first. + // It was also requested to check a specific bundle for this entity. if (isset($bundle)) { - // Allow entities to be disabled by assigning a variable - // 'metatag_enable_{$entity_type}__{$bundle}' the value FALSE, e.g.: - // - // // Disable metatags for carousel nodes. - // $conf['metatag_enable_node__carousel'] = FALSE; - // - // @see Advanced settings page. - if (variable_get('metatag_enable_' . $entity_type . '__' . $bundle, 'monkey') == FALSE) { - return FALSE; - } - - return isset($entity_types[$entity_type][$bundle]) ? $entity_types[$entity_type][$bundle] : FALSE; + return !empty($entity_types[$entity_type][$bundle]); } - - // Otherwise check the entity type itself. + // Check the entity. else { - // Allow entities to be disabled by assigning a variable - // 'metatag_enable_{$entity_type}' the value FALSE, e.g.: - // - // // Disable metatags for file_entity. - // $conf['metatag_enable_file'] = FALSE; - // - // @see Advanced settings page. - if (variable_get('metatag_enable_' . $entity_type, 'monkey') == FALSE) { - return FALSE; - } - - return isset($entity_types[$entity_type]) ? ($entity_types[$entity_type] !== FALSE) : FALSE; + return !empty($entity_types[$entity_type]); } } + // If nothing specific was requested, return the complete list of supported + // entities & bundles. return $entity_types; } @@ -1426,6 +1432,8 @@ function metatag_entity_supports_metatags($entity_type = NULL, $bundle = NULL) { * Implements hook_entity_info_alter(). * * Enables Metatag support for the core entities. + * + * Deprecated, will be removed in the next release. */ function metatag_entity_info_alter(&$info) { $defaults['node'] = array( @@ -2349,3 +2357,35 @@ function metatag_admin_menu_cache_info() { ); return $caches; } + +/** + * Identify whether an entity type is technically capable of having meta tags. + * + * In order to be capable of having meta tags, an entity type must have view + * modes and may not be a configuration entity. + * + * @param string $entity_type + * @param array $entity_info + * + * @return bool + */ +function metatag_entity_type_is_suitable($entity_type, $entity_info = array()) { + $suitable = TRUE; + + // If the entity info was not passed along, load it. + if (empty($entity_info)) { + $entity_info = entity_get_info($entity_type); + } + + // Configuration entities may not have meta tags. + if (isset($entity_info['configuration']) && $entity_info['configuration'] == TRUE) { + $suitable = FALSE; + } + + // There must be view modes. + elseif (empty($entity_info['view modes'])) { + $suitable = FALSE; + } + + return $suitable; +}