Lagrangian description of perfect fluids and modified gravity with an extra force