0

I am actualy trying to implement my first own entity using this tutorial:

http://www.istos.it/en/blog/drupal-entities/drupal-entities-part-3-programming-hello-drupal-entity

So far anything worked fine, but now i ran in some problem I couldn't get fixed. After creating my new entity, I get redirected to the page "slider/(id)". But it says that the page could not be found. When I try to open the URL manualy I get the same error.

Here is my code: he_slider.module

/**
    * Implements hook_entitiy_info()
    */

function he_slider_entity_info(){
    $slider_info['slider'] = array(
        'label' => t('Slider Item'),
        'controller class' => 'SliderController',
        'views controller class' => 'EntityDefaultViewsController',
        'base table' => 'slider',
        'uri callback' => 'he_slider_uri',
        'fieldable' => TRUE,
        'entity keys' => array(
            'id' => 'sid',
        ),
        'static cache' => TRUE,
        'bundles' => array(
            'slider'=> array(
                'label' => 'Slider',
                'admin' => array(
                    'path' => 'admin/structure/slider/manage',
                    'access arguments' => array('administer slider'),
                ),
            ),
        ),
        'view modes' => array(
            'full' => array(
                'label' => t('Full Slider Item'),
                'custom settings' =>  FALSE,
            ),
        )
    );

    return $slider_info;
}

/**
    * URI Callback function
    */

function he_slider_uri($slider){
    return array(
        'path' => 'slider/' . $slider->id,
    );
}

/**
    *  Load the a single or multiple entities
    */

function he_slider_load($sid = NULL, $reset = FALSE){
    $sids = (isset ($sid) ? array($sid) : array());
    $slider = slider_load_multiple($sids, $reset);
    return $slider? reset ($slider) : FALSE;
}

function he_slider_load_multiple($sids = array(), $conditions = array(), $reset = FALSE){
    return entity_load('slider', $sids, $conditions, $reset);
}


/**
    * Provides the Pages to administer and view the entities
    */

function he_slider_menu(){
    $items['admin/content/slider/add'] = array(
        'title' => 'Add Slider Item',
        'page callback' => 'he_slider_add',
        'access arguments' => array('create slideritem'),
    );
    $items['admin/structure/slider/manage'] = array(
        'title' => 'Slider Administration',
        'description' => 'Manage Slider Structure',
        'page callback' => 'he_slider_info',
        'access arguments' => array('administer slideritem'),
    );
    $items['slider/%slider'] = array(
        'title callback' => 'he_slider_page_title',
        'title arguments' => array(1),
        'page callback' => 'he_slider_page_view',
        'page arguments' => array(1),
        'access arguments' => array('view slideritem'),
        'type' => MENU_CALLBACK,
    );
    $items['slider/%slider/edit'] = array(
        'title' => 'Slider Edit',
        'title arguments' => array(1),
        'page callback' => 'he_slider_page_edit',
        'page arguments' => array(1),
         'access arguments' => array('edit slideritem'),
        'type' => MENU_CALLBACK,
    );
    $items['admin/content/slider/add'] = array(
        'title' => 'Add Slider Item!',
        'page callback' => 'he_slider_add',
        'access arguments' => array('create slideritem'),
    );
    return $items;
}

function he_slider_permission(){
        return array(
        'administer slideritem' =>  array(
            'title' => t('Administer Slider'),
            'restrict access' => TRUE,
        ),
        'view slideritem' => array(
            'title' => t('View Slideritems'),
        ),
        'edit slideritem' => array(
            'title' => t('Edit Slideritems'),
        )
    );
}


function he_slider_info() {
    return ('Welcome to the administration page for your Slideritems!');
}

function he_slider_page_title($slider){
    return $slider->sid;
}

function he_slider_page_view($slider, $view_mode = 'full'){
    $slider->content = array();

    // Build fields content.
    field_attach_prepare_view('slider', array($slider->sid => $slider), $view_mode);
    entity_prepare_view('slider', array($slider->sid => $slider));
    $slider->content += field_attach_view('slider', $slider, $view_mode);

    return $slider->content;
}


function he_slider_field_extra_fields() {
    $return = array();
    $return['slider']['slider'] = array(
        'form' => array(
            'title' => array(
                'label' => t('Title'),
                'description' => t('Slider Title'),
            ),
        ),
    );


    return $return;
}

/**
    * Implements the slider add form, where we are able to create our new entities
    */

function he_slider_add() {
    $slider = (object) array (
        'sid' => '',
        'type' => 'slider',
        'title' => '',
    );

    return drupal_get_form('he_slider_add_form', $slider);
}


function he_slider_add_form($form, &$form_state, $slider) {
    $form['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Note'),
        '#required' => TRUE,
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
    );

    field_attach_form('slider', $slider, $form, $form_state);

    return $form;
}

/**
    * Implements the Entitiy Edit form
    */

function he_slider_page_edit($slider) {
    return drupal_get_form('he_slider_add_form', $slider);
}

/**
    * Validates the Entity Add form
    */

function he_slider_add_form_validate($form, &$form_state) {
    $slider_submission = (object) $form_state['values'];
    field_attach_form_validate('slider', $slider_submission, $form, $form_state);
}

function he_slider_add_form_submit($form, &$form_state) {
    $slider_submission = (object) $form_state['values'];
    field_attach_submit('slider', $slider_submission, $form, $form_state);
    $slider = he_slider_save($slider_submission);
    $form_state['redirect'] = "slider/$slider->sid";
}

function he_slider_save(&$slider) {
    return entity_get_controller('slider')->save($slider);
}

/**
 * Implements hook_views_api().
 */
function he_slider_views_api() {
    return array(
        'api' => 3,
        'path' => drupal_get_path('module', 'slider') . '/views',
    );
}

/**
* Implements hook_form_alter().
*/
function he_slider_form_alter(&$form, &$form_state, $form_id) {
    if($form_id=='_slider_add_form'){
        $form['submit']['#weight'] = 99;
    }
}

he_slider.controller.inc:

class SliderController extends DrupalDefaultEntityController {
    public function save($slider) {
        drupal_write_record('slider', $slider);
        field_attach_insert('slider', $slider);
        module_invoke_all('entity_insert', $slider, 'slider');
        return $slider;
    }
}
0

Got help on stackoverflow.com

Quote from Clive:

The problem is here:

$items['slider/%slider'] = array(

Drupal's menu system will look for a function named slider_load(), whereas your entity load function is named he_slider_load(). This causes the router to bail out and throw a 404.

To fix it, simply change that line to

$items['slider/%he_slider'] = array(

And clear your caches.

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.