0

What I'm doing is complicated, and there is likely to be a much better way to do it, so I'll try to go into some detail, and edit the question after the fact to tailor more closely to the final answer I get if the answer is to do something different.

Right now for my teaser view for nodes I generate a generated_cover field in code to allow me to fallback through a couple of potential sources of cover image. First there is a cover image field. If a user has uploaded a cover image for the node we use that. Second is a cover generated by the pdfpreview module, finally if neither of those are found, it returns a blank, which I then use with field formatter conditions in display suite to replace with a placeholder image. As you'll note from the code used for generating the fields below you'll note that they rely on `$field['build'] to get the generated field.

/**
 * Implements hook_ds_fields_info().
 */
function MY_MODULE_ds_fields_info($entity_type) {
    if ($entity_type == 'node') {
        $fields = array();
        $fields['generated_cover'] = array(
            'title' => 'Generated Cover',
            'field_type' => DS_FIELD_TYPE_FUNCTION,
            'function' => '_MY_MODULE_ds_field_generated_cover',
            'formatters' => array(
                'standard' => 'Standard output',
            ),
        );
        return array('node' => $fields,);
    }
}

/*
 * Generate the output for the generated cover image field
 */
function _MY_MODULE_ds_field_generated_cover($field) {

    // Get information about the entity being rated and the formatter we're using.
    $entity = $field['entity'];
    $formatter = $field['formatter'];

    if (!empty($entity->field_doc_cover)) {
//        $output = "cover";
        $output = render($field['build']['field_doc_cover']);
    //    $output = render(entity_view('doc_publication_files', $entity->field_doc_cover, 'teaser', $entity->language));
    } else if (!empty($entity->field_doc_pub_files)) {
//        $output = "preview";
        $output = render($field['build']['field_doc_pub_files'][0]);
    } else {
        $output = "";
    }
    return $output;
}

This has worked fine for the teaser view, though the build variable is never populated if the field is disabled for the display, so I need to include the fields in the display and use field formatter conditions to hide them. Now I need to use the same fallback on the full display, and am running into an issue: I need to display the file field using a different formatter AND have the pdfpreview version as an option for the cover fallback. I used a dynamic field to bring a copy of the file field in and set up the formatter as I needed it to be, while still keeping the original file field as a hidden field set up to the pdfpreview format, but now no matter how I try to hide the original file field (with the pdfpreview formatter) and display the dynamic field, if the pdfpreview version is not completely disabled, the dynamic version will not show up.

I'm trying to fix this by not relying on the presence of the original field in my generated form anymore. I'd like _MY_MODULE_ds_field_generated_cover to produce and render an appropriate render array for the cover image if it exists (image at medium style linking to nothing) and produce and render an appropriate pdf preview for the first file if it exists and is a pdf file (again medium style). Otherwise it should return nothing. That way I'll be able to use my original file field in my displays without concern that they'll interfere with each other.

1

I got it worked out, so here is my new code that actually renders the image fields instead of relying on them being prerendered for me:

/**
 * Implements hook_ds_fields_info().
 */
function MY_MODULE_ds_fields_info($entity_type) {
    if ($entity_type == 'node') {
        $fields = array();
        $fields['generated_cover'] = array(
            'title' => 'Generated Cover',
            'field_type' => DS_FIELD_TYPE_FUNCTION,
            'function' => '_MY_MODULE_ds_field_generated_cover',
            'formatters' => array(
                'standard' => 'Standard output',
            ),
        );
        return array('node' => $fields,);
    }
}

/*
 * Generate the output for the generated cover image field
 */
function _MY_MODULE_ds_field_generated_cover($field) {

    // Get information about the entity being rated and the formatter we're using.
    $entity = $field['entity'];
    $image_style = ($field['view_mode'] == 'teaser') ? 'list_view_cover' : 'medium';

    if (!empty($entity->field_doc_cover)) {
//        $output = "cover";
        $display = array(
            'settings' => array(
                'image_style' => $image_style,
            ),
            'label' => 'hidden',
        );
        $to_render = field_view_field('node', $entity, 'field_doc_cover', $display);
        $output = render($to_render[0]);

    //    $output = render(entity_view('doc_publication_files', $entity->field_doc_cover, 'teaser', $entity->language));
    } else if (!empty($entity->field_doc_pub_files)) {
//        $output = "preview";
        $display = array(
            'settings' => array(
                'image_style' => $image_style,
            ),
            'type' => 'pdfpreview',
            'label' => 'hidden',
        );
        $to_render = field_view_field('node', $entity, 'field_doc_pub_files', $display);
        $output = render($to_render[0]);
    } else {
        $output = "";
    }
    return $output;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.