0

as it said on the title, I've create a custom table named : writers (contains name,books..of writers)

To add a book, I've created also a content type that contains a form, with many textfields (book title, editor, number of pages..ect),now I want to have a list of writers (that are stored on my table : writers) to be sure that the book I'm adding is a book written by one of the writers I've on my table

I'm working on it for 2 days and I don't know how to do it, please if anyone can helps me it would be great

P.S. I don't want to let the user write the name of the writer on the form (content type) I want to let him choose on a list

Thank you

This is my book.module (to get an item to access to the form book):

<?php


function book_menu (){

$items['admin/books'] = array(
    'title' => 'books',
    'description' => 'books form',
    'title callback' => 'book_title_callback',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array('administer books'),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('book_form')
);

  return $items;
 }

function book_permission(){
  return array(
   'administer books' => array(
   'title' => t('permissions to deal with books'),
   'description' => t('permissions to deal with books'),
 ),
  );
  }

and I have some functions..

Here we have the new function hook_update_7001 :

function book_update_7001(&$sandbox){
$info = field_info_field('field_data_field_writer'); //the select list on the content type

  $result = db_query("SELECT name_writer, uid FROM {writers} ");

  $res = $result->execute();
  $options = array();
   foreach($res as $sql){
    $options[] = array(
        'uid' => $sql-> uid,
        'name' => $sql-> name_writer,
        );
   }
$info['settings']['allowed_values'] = $options;
field_update_field($info);
 }
0

Did you already try the Entity reference module? If I understand your question correctly, you could create a content type Writer and add a reference field on your Book content type. This reference field can be used to select (i.e., use the 'Select list' widget) the appropriate Writer for your Book. No need to create a custom table yourself.

With permissions you can control who's allowed to create/edit/delete each of the content types.

  • Hi and thak you for helping me ^^ It's a good idea but I have to create a custom table for other reasons – Mitch Jul 8 '15 at 11:00
0

You can add new field to "Book" content type with type "Select list". And then using hook_update_n add options to this field.

function mymodule_update_7001(&$sandbox) {
  // Get the field info
  $info = field_info_field('field_some_field');

  $options = array();
  //Make a select from your 'writers' table and prepare an array with writers.
  $query = db_select('writers');
  ...

  // Save the field
  $info['settings']['allowed_values'] = $options;
  field_update_field($info);
}
  • Hi Andrew & thank you for you answer, It seems to be what I'm looking for, but where should I implement the hook_update ? In the book.module ? – Mitch Jul 8 '15 at 11:01
  • Hi Pit. It is better to add your custom module for this – Andrew Jul 8 '15 at 11:11
  • And for the field_info_field, should I set my custom table name ? (writers ?) – Mitch Jul 8 '15 at 11:24
  • You should set there field name from your content type – Andrew Jul 8 '15 at 11:27
  • I've just implement this function on the book.module but the select list that i've add on the content type doesn't show the writers list – Mitch Jul 8 '15 at 12:52

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.