danielebarchiesi@0: t("Entity: bundle"), danielebarchiesi@0: 'description' => t('Control access by entity bundle.'), danielebarchiesi@0: 'callback' => 'ctools_entity_bundle_ctools_access_check', danielebarchiesi@0: 'default' => array('type' => array()), danielebarchiesi@0: 'settings form' => 'ctools_entity_bundle_ctools_access_settings', danielebarchiesi@0: 'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit', danielebarchiesi@0: 'summary' => 'ctools_entity_bundle_ctools_access_summary', danielebarchiesi@0: 'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions', danielebarchiesi@0: 'get child' => 'ctools_entity_bundle_ctools_access_get_child', danielebarchiesi@0: 'get children' => 'ctools_entity_bundle_ctools_access_get_children', danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) { danielebarchiesi@0: $plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent); danielebarchiesi@0: return $plugins[$parent . ':' . $child]; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) { danielebarchiesi@0: $entities = entity_get_info(); danielebarchiesi@0: $plugins = array(); danielebarchiesi@0: foreach ($entities as $entity_type => $entity) { danielebarchiesi@0: $plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label'])); danielebarchiesi@0: $plugin['keyword'] = $entity_type; danielebarchiesi@0: $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type)); danielebarchiesi@0: $plugin['name'] = $parent . ':' . $entity_type; danielebarchiesi@0: $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type); danielebarchiesi@0: $plugins[$parent . ':' . $entity_type] = $plugin; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return $plugins; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Settings form for the 'by entity_bundle' access plugin danielebarchiesi@0: */ danielebarchiesi@0: function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) { danielebarchiesi@0: $plugin = $form_state['plugin']; danielebarchiesi@0: $entity_type = explode(':', $plugin['name']); danielebarchiesi@0: $entity_type = $entity_type[1]; danielebarchiesi@0: $entity = entity_get_info($entity_type); danielebarchiesi@0: foreach ($entity['bundles'] as $type => $info) { danielebarchiesi@0: $options[$type] = check_plain($info['label']); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: $form['settings']['type'] = array( danielebarchiesi@0: '#title' => t('Entity Bundle'), danielebarchiesi@0: '#type' => 'checkboxes', danielebarchiesi@0: '#options' => $options, danielebarchiesi@0: '#description' => t('Only the checked entity bundles will be valid.'), danielebarchiesi@0: '#default_value' => $conf['type'], danielebarchiesi@0: ); danielebarchiesi@0: return $form; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Compress the entity bundles allowed to the minimum. danielebarchiesi@0: */ danielebarchiesi@0: function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) { danielebarchiesi@0: $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Check for access. danielebarchiesi@0: */ danielebarchiesi@0: function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) { danielebarchiesi@0: list($plugin_name, $entity_type) = explode(':', $plugin['name']); danielebarchiesi@0: if (!$entity_type) { danielebarchiesi@0: return FALSE; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: $entity = entity_get_info($entity_type); danielebarchiesi@0: // As far as I know there should always be a context at this point, but this danielebarchiesi@0: // is safe. danielebarchiesi@0: if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) { danielebarchiesi@0: return FALSE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return TRUE; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Inform the UI that we've eliminated a bunch of possibilities for this danielebarchiesi@0: * context. danielebarchiesi@0: */ danielebarchiesi@0: function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) { danielebarchiesi@0: if (isset($context->restrictions['type'])) { danielebarchiesi@0: $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type'])))); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $context->restrictions['type'] = array_keys(array_filter($conf['type'])); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Provide a summary description based upon the checked entity_bundle. danielebarchiesi@0: */ danielebarchiesi@0: function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) { danielebarchiesi@0: if (!isset($conf['type'])) { danielebarchiesi@0: $conf['type'] = array(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: list($plugin_name, $entity_type) = explode(':', $plugin['name']); danielebarchiesi@0: if (!$entity_type) { danielebarchiesi@0: return t('Error, misconfigured entity_bundle access plugin'); danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: $entity = entity_get_info($entity_type); danielebarchiesi@0: danielebarchiesi@0: $names = array(); danielebarchiesi@0: foreach (array_filter($conf['type']) as $type) { danielebarchiesi@0: $names[] = check_plain($entity['bundles'][$type]['label']); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (empty($names)) { danielebarchiesi@0: return t('@identifier is any bundle', array('@identifier' => $context->identifier)); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier)); danielebarchiesi@0: } danielebarchiesi@0: