Commit bb4f5e3d authored by aDarkling's avatar aDarkling Committed by Devin Carlson

Issue #2634718 by aDarkling, Devin Carlson: Display Option list not restricted...

Issue #2634718 by aDarkling, Devin Carlson: Display Option list not restricted to allowed configuration
parent 9780a088
......@@ -170,48 +170,10 @@ function entity_embed_dialog_form($form, &$form_state, $filter_format, $embed_bu
'#value' => $entity_element['data-entity-uuid'],
);
$formatter_options = array();
$available_plugins = array();
// Retrieve a list of formatters appropriate for the currently selected
// entity type.
$formatters = entity_embed_get_entity_field_formatters($entity_element['data-entity-type']);
if (!empty($formatters)) {
foreach ($formatters as $formatter => $info) {
$available_plugins[$info['module'] . ':' . $formatter] = $info;
$formatter_options[$info['module'] . ':' . $formatter] = $info['label'];
// With the File Entity module enabled, file entities require some
// special handling. Unlike standard field formatters which specify a
// list of compatible entity types and will work with any entity as
// long as it is the correct type, file formatters may only support
// files with specific MIME types.
if ($entity_element['data-entity-type'] == 'file' && module_exists('file_entity')) {
if (isset($info['file formatter']['mime types'])) {
if (!file_entity_match_mimetypes($info['file formatter']['mime types'], $entity->filemime)) {
// Remove the incompatible formatter.
unset($formatter_options[$info['module'] . ':' . $formatter]);
}
}
}
// With the File Image Formatters module enabled, image field
// formatters will appear twice.
if ($info['module'] == 'file_image_formatters' && module_exists('file_image_formatters')) {
// Remove the duplicate formatter.
unset($formatter_options[$info['module'] . ':' . $formatter]);
}
}
}
// The 'Rendered entity' formatter can not be used for files unless
// the file_entity module is available.
if (!empty($formatter_options['entityreference:entityreference_entity_view']) && $entity_element['data-entity-type'] == 'file' && !module_exists('file_entity')) {
unset($formatter_options['entityreference:entityreference_entity_view']);
}
// Determine the available Display plugins for the current entity type.
$available_plugins = entity_embed_get_entity_field_formatters($entity_element['data-entity-type'], $entity);
// Build the list of allowed Entity Embed Display plugins.
// Retrieve the list of allowed Entity Embed Display plugins.
$allowed_plugins = $embed_button->display_plugins;
// If list of allowed options is empty, it means that all plugins are
......@@ -236,7 +198,7 @@ function entity_embed_dialog_form($form, &$form_state, $filter_format, $embed_bu
$form['attributes']['data-entity-embed-display'] = array(
'#type' => 'select',
'#title' => t('Display as'),
'#options' => $formatter_options,
'#options' => $display_plugin_options,
'#default_value' => $entity_element['data-entity-embed-display'],
'#required' => TRUE,
'#ajax' => array(
......@@ -393,9 +355,16 @@ function entity_embed_dialog_form_validate($form, &$form_state) {
// Ensure that at least one Entity Embed Display plugin is present
// before proceeding to the next step. Rasie an error otherwise.
$embed_button = $form['#embed_button'];
// Determine the available Display plugins for the current entity type.
$available_plugins = entity_embed_get_entity_field_formatters($entity_type, $entity);
// Retrieve the list of allowed Entity Embed Display plugins.
$allowed_plugins = $embed_button->display_plugins;
$available_plugins = entity_embed_get_entity_field_formatters($entity_type);
// If list of allowed options is empty, it means that all plugins are
// allowed. Else, take the intersection of allowed and available
// plugins.
$display_plugin_options = empty($allowed_plugins) ? $available_plugins : array_intersect_key($available_plugins, $allowed_plugins);
// If no plugin is available after taking the intersection,
......
......@@ -211,17 +211,59 @@ function entity_embed_get_entity_field_types($entity_type) {
/**
* Helper function to get all the field formatters that we can use to display
* entity types.
*
* @param $entity_type
* The type of entity, i.e. 'node', 'user'.
* @param $entity
* (optional) The entity to be rendered. This is used to perform special
* checks/processing for unruly modules.
*
* @return array
*/
function entity_embed_get_entity_field_formatters($entity_type) {
function entity_embed_get_entity_field_formatters($entity_type, $entity = NULL) {
$formatters = field_info_formatter_types();
$field_types = entity_embed_get_entity_field_types($entity_type);
$entity_formatters = array();
foreach ($formatters as $formatter => $info) {
// Files require special handling.
if ($entity_type == 'file') {
// Files behave differently depending on whether the File Entity module is
// available.
if (module_exists('file_entity')) {
// With the File Entity module enabled, file entities require some
// special handling. Unlike standard field formatters which specify a
// list of compatible entity types and will work with any entity as long
// as it is the correct type, file formatters may only support files
// with specific MIME types.
if (isset($entity) && isset($info['file formatter']['mime types']) && isset($entity->filemime)) {
if (!file_entity_match_mimetypes($info['file formatter']['mime types'], $entity->filemime)) {
// Skip the incompatible formatter.
continue;
}
}
}
else {
// The 'Rendered entity' formatter can not be used for files unless the
// File Entity module is available.
if ($formatter == 'entityreference_entity_view') {
// Skip the incompatible formatter.
continue;
}
}
}
// With the File Image Formatters module enabled, image field formatters
// will appear twice.
if ($info['module'] == 'file_image_formatters' && module_exists('file_image_formatters')) {
// Skip the duplicate formatter.
continue;
}
foreach ($info['field types'] as $field_type) {
if (in_array($field_type, $field_types)) {
$entity_formatters[$formatter] = $info;
$entity_formatters[$info['module'] . ':' . $formatter] = $info['label'];
break;
}
}
......
......@@ -96,13 +96,7 @@ function entity_embed_ctools_export_ui_form(&$form, &$form_state) {
// Retrieve a list of formatters appropriate for the currently selected
// entity type.
$formatters = entity_embed_get_entity_field_formatters($selected);
if (!empty($formatters)) {
foreach ($formatters as $formatter => $info) {
$formatter_options[$info['module'] . ':' . $formatter] = $info['label'];
}
}
$formatter_options = entity_embed_get_entity_field_formatters($selected);
}
if (isset($options[$selected])) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment